U E D R , A S I H C RSS

How Many Zeros And Digits/문보창

소감

시간제한이 1분짜리 문제다. Digits의 개수를 세는 것은 로그를 이용하면 간단히 해결되나, Zeros의 개수를 세는 방법이 딱히 떠오르지 않는다.

코드

~cpp 
// no10061 - How many zeros and how many digits?
#include <iostream> 
#include <cmath> 
using namespace std; 

int main() 
{                
	double N;				// number
	double B;				// base
	double i;
	double nDigit;			// how many digits?
	int nZero;				// how many zeros?
	double temp;
	double backTemp;
	while (cin >> N >> B)
	{
		nZero = 0;
		nDigit = 0.0;
		backTemp = 1;
		for (i=2; i<=N; i++)
		{
			nDigit += log(i) / log(B);
			temp = backTemp * i;
			while (true)
			{
				nZero++;
				temp = int(temp/B);
			}
			backTemp = int(i);
		}
		cout << nZero << " " << int(nDigit) + 1 << endl;
	}
	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:25
Processing time 0.0247 sec