Big Number
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 3
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[sup]7[/sup] 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
Source
Asia 2002, Dhaka (Bengal)
题意:求n!的位数
难点:n!是一个很大的数
思路:每乘一次阶乘数,如果发现结果大于10,就除到值是一个小于10的数为止,同时每执行这样让一个大数变成一个小于10的数的过程中count要加1,最后的结果再加上个位数的1个即可
代码:
#include<stdio.h>
int main(){
int n,t,cnt;
double sum;
scanf("%d",&n);
while(n--){
scanf("%d",&t);
sum=1;//sum为计算阶乘的结果
cnt=0;//结果的位数
for(int i=2;i<=t;i++){
sum*=i*1.0;
while(sum>=10){
cnt++;
sum=sum*1.0/10;//每当此次几个阶乘数的积大于10的n倍后将他缩小n/10倍,防止溢出
}
}
printf("%d\n",cnt+1);//记得要加上个位数
}
return 0;
}