Big Number大数目
Problem Description
In manyapplications very large integers numbers are required. Some of theseapplications are using keys for secure transmission of data, encryption, etc.In this problem you are given a number, you have to determine the number ofdigits in the factorial of the number.
在许多应用中非常大的整数数字是必需的。这些应用使用密钥数据,加密等安全传输在这个问题中,你将得到一个号码,则必须确定在数的阶乘的位数。
Input
Input consists ofseveral lines of integer numbers. The first line contains an integer n, whichis the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤107 on each line.
输入由若干行整数数字。第一行包含一个整数n,这是要测试的情况下的数量,后跟n线路,一个整数1≤N≤107上的每一行。
Output
The outputcontains the number of digits in the factorial of the integers appearing in theinput.
输出包含的位数出现在输入的整数的阶乘的数目。
Sample Input
2
10
20
Sample Output
7
19
代码如下:
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#include<stdio.h>
#include<math.h>
//这个题的意思是:给你一个数,让你求出N!由多少位数构成,比如输出10,它的阶乖是3628800 由7位数构成,这时你要输出7;
//解题思路:
//1.可以暴力,N的阶乖的位数等于LOG10(N!)=LOG10(1)+.....LOG10(N);
//2.Stirling公式:n!与√(2πn) * n^n * e^(-n)的值十分接近
//故log10(n!) = log(n!) / log(10) = ( n*log(n) - n + 0.5*log(2*π*n))/log(n);
//
//解法一:
//LANGUAGE:C++
//CODE:
double reback(int n) //计算阶乘的位数
{
int i;
double cnt=0;
for(i=2;i<=n;i++)
{
cnt+=log10(i); //把求阶乘转化成加法运算
}
return cnt;
}
int main()
{
int group,n;
scanf("%d",&group);
while(group--)
{
scanf("%d",&n);
printf("%d\n",(int)reback(n)+1);
}
return 0;
}