Big Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 39915 Accepted Submission(s): 19434
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 ≤ 10
7 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
1.斯特林公式:N!=sqrt(2*pi*n)*pow((n/e),n);
N!的位数=log10(2*pi*n)/2+n*log10(n/e)+1;
#include <stdio.h>
#include<math.h>
double pi=3.14159265358;
double e=2.71828182;
int main(int argc, char *argv[])
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int ans=log10(2*pi*n)/2+n*log10(n/e)+1;
printf("%d\n",ans);
}
return 0;
}
2.暴力:每次超过10就开始去掉当前的数位 注意!!!!存阶乘的sum要用 double用long long会wa
#include <stdio.h>
int main(int argc, char *argv[])
{
int t;
scanf("%d",&t);
while(t--)
{
long long n;
scanf("%lld",&n);
double sum=1;
int i;
int cnt=1;
for(i=2;i<=n;i++)
{
sum=sum*i;
if(sum>=10)
{
while(sum>=10)
{
sum/=10;
cnt++;
}
}
}
printf("%d\n",cnt);
}
return 0;
}