您的位置:首页 > 其它

HDU 1018 Big Number

2014-04-06 18:45 423 查看


Big Number

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 24016 Accepted Submission(s): 10884



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(1<=n<=10^7),求n!的位数。很明显转化为对数就ok了。

注意:一个数x的位数等于用(int)log10(x)+1,所以x=n! 的位数即为log10(x)也就是log10(i) (1<=i<=n)整数部分+1.

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

int gao(int num){
	double ans = 0;
	int i;
	for(i=2;i<=num;++i){
		ans += log10(i*1.0);
	}
	return ans;
}

int main(){

	int n,num,res;
	scanf("%d",&n);
	while(n--){
		scanf("%d",&num);
		res = gao(num);
		printf("%d\n",res+1);
	}
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: