Big Number
*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38918 Accepted Submission(s): 18910*
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,输出 n的阶乘 是几位数。
例如当n=10时,10! = 3628800 , 所以输出 7。
思路:
把n!算出来理论上可行,但是考虑到时间、空间限制,不能采取这种方法。
用log函数解题:
m的位数 = log10(m)+1
注:这里的n要是double型,本题的数据类型为int型,所以在使用这个函数的时候,要先将其强制转化为double型。
log10(n!) = log10(1)+log10(2)+·········+log10(n)+1
C++代码:
#include <cstdio>
#include <math.h>
int main(){
int n,temp;
double sum;
scanf("%d",&n);
for(int i = 0 ; i < n ; i ++){
sum = 0;
scanf("%d",&temp);
for(int j = 1 ; j <= temp ; j++)
sum = sum + log10((double)j);
printf("%d\n",(int)sum+1);
}
return 0;
}