N! (N factorial) can be quite irritating and difficult to compute for large values of N. So instead of calculating N!, I want to know how many digits are in it. (Remember that N! = N * (N - 1) * (N - 2) * ... * 2 * 1)
Each line of the input will have a single integer N on it 0 < N < 1000000 (1 million). Input is terminated by end of file.
For each value of N, print out how many digits are in N!.
1 3 32000
1 1 130271
题目大意:
给你一个数N求N!有多少位
有了思路很简单
首先我们明确,求一个数有几位最快的方法是开lg
比如100 开lg为2,所以有2+1位
199开lg(注意只要整数)为2,所以有2+1位
那么lgN!=lg(1*2*3*......*N)=lg1+lg2+lg3+.....+lgN;
所以!不就出来了
下面20行代码奉上~
#include<stdio.h>
#include<math.h>
int main()
{
int n;
while(~scanf("%d",&n))
{
double sum=0;
int x;
for(int i=1;i<=n;i++)
{
sum+=log10((double)i);
x=(int)sum;
}
printf("%d\n",x+1);
}
return 0;
}