HDOJ-1018 Big Number

http://acm.hdu.edu.cn/showproblem.php?pid=1018

题意:给出一个数n,输出n的阶乘的位数

汗Σ( ° △ °|||)︴
刚开始还准备上大数乘法 然而10000的阶乘结果就已经接近40000位
10^7的阶乘...

正:对于一个数n 求其位数可以用 log10(n) + 1 求得
所以 对于N! 其位数= log10(1*2*...*(N-1)*N) +1 = log10(1)+log10(2)+..+log10(N-1)+log10(N) + 1
【简单粗暴

# include <stdio.h>
# include <math.h>

int main()
{
	int t, num;
	scanf("%d", &t);
	while(t--)
	{
		scanf("%d", &num);
		double len = 0;
		for(int i = 1; i <= num; i++)
		{
			len += log10((double)i);
		}
		printf("%d\n",(int)len + 1);
	}

	return 0;
}

  

posted @ 2015-04-28 19:47  懒人の猫  阅读(112)  评论(0编辑  收藏  举报