Big Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38886 Accepted Submission(s):
18889
Problem Description
In many applications very large integers numbers are
required. Some of these applications are using keys for secure transmission of
data, encryption, etc. In this problem you are given a number, you have to
determine the number of digits in the factorial of the number.
Input
Input consists of several lines of integer numbers. The
first line contains an integer n, which is the number of cases to be tested,
followed by n lines, one integer 1 ≤ n ≤ 107 on each line.
Output
The output contains the number of digits in the
factorial of the integers appearing in the input.
Sample Input
2
10
20
Sample Output
7
19
刚看到这道题,其实就应该知道,不会是简单的用一个阶乘函数来解答,因为它的数值太大了;
所以,我们可以换个思路:
//求一个超大数阶乘的位数
//所谓n!的十进制位数,就是 log(n)+1, 根据数学公式有:n!=1*2*3*.....*n;
//lg(n!)=lg(2)+......lg(n);
//则位数 = log(n)+1;
代码如下:
#include<stdio.h>
#include<math.h>
double f(int n)
{
double cnt=0;
for(double i=2;i<=n;i++) // i也要为double型,否则会出现歧义 !
{
cnt += log10(i);
}
return cnt;
}
int main()
{
int cas,n;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
printf("%d\n",(int)f(n) + 1);
}
return 0;
}