杭电1018Realtime Status
Problem Description
In many applications verylarge integers numbers are required. Some of these applications are using keysfor secure transmission of data, encryption, etc. In this problem you are givena number, you have to determine the number of digits in the factorial of thenumber.
Input
Input consists of severallines of integer numbers. The first line contains an integer n, which is thenumber of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 107on each line.
Output
The output contains the numberof digits in the factorial of the integers appearing in the input.
Sample Input
2
10
20
Sample Output
7
19
1;介绍题目大意;
contains the number of digits……是说这个数的位数是多少的意思
factoria……………………………阶乘的意思;
大意就是求一个小于10^7方的大数,该数阶乘的位数是多少;
2;看到题目是不是很方;跟我一样;然而解答却十分吓人;
介绍一个公式;
……任意一个正整数a的位数都等于(int)log10(a) + 1;//关键;
很惊讶吧;让我来推导一遍吧;
10^(x-1) <= a < 10^x;……可以看出x就是a的位数吧;
…log10(10^(x-1)) <= log10(a) < log10(10^x);
……x-1 <= log10(a) < x;
………………(int)log10(a) = x-1;
…………………… x = (int)log10(a) +1;
公式就介绍到这里;在看看这题与我们公式有什么关系呢;
题目是求n的阶乘的位数;
我们公式是求的a的位数;
然而我们难道不鞥吧a ===== n!;一切都明目了吧;
还有一个小公式,不知道大家还记得吗;
Log10(1*2*3*4*5*6*…………*n)
=log10(1)+ log10(2)+…………log10(n)
现在我们就可以轻松ac了;开始敲代码吧;
#include<stdio.h>
#include<math.h>
int main()
{
long long int n, a, i;
double num;
scanf("%lld",&n);
while(n--){
num = 0;
scanf("%lld",&a);
for(i = 1; i <= a;i++){
num += log10(i*1.0);
}
printf("%d\n",(int)num+1);
}
return 0 ;
}