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!的位数
方法1:一个数n的位数可以用1+log10(n)求得
代码如下:
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
int main()
{
int t,n,i;
double sum;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
sum=1;
for(i=1;i<=n;i++)
sum+=log10(i);
cout<<(int)sum<<endl;
}
return 0;
}
Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language |
1018 | 984MS | 340K | 328 B | G++ |
方法2:斯特林公式
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14159265
int main()
{
int n,t;
cin>>t;
while(t--)
{
cin>>n;
cout<<(int)((0.5*log(PI*2*n)+n*log(n)-n)/log(10))+1<<endl;
}
return 0;
}
Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
Accepted | 1018 | 0MS | 344K | 259 B | G++ | Array |