首页 > 代码库 > hdu 1018 Big Number

hdu 1018 Big Number

Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25567    Accepted Submission(s): 11600


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!的位数
=[lg(N!)]+1=[lg(1)+lg(2)+…+lg(N)]+1            //[ ]符号为取整
=(int)ceil((n*ln(n)-n+0.5*ln(2*n*π))/ln(10))   // ceil是向上取整

拓展:
// ceil(n) 取大于等于数值n的最小整数
// floor(n)取小于等于数值n的最大整数 
// 对于一个B进制的数,只需要对其取以B的对数就可以得到他在B进制情况下的位数(取了对数之后可能为小数,所以还需要取整后再+1)


<span style="font-family:FangSong_GB2312;font-size:24px;"><strong>#include<stdio.h>
#include<math.h>
int main ()
{
	int i,j;
	int n,a;
	double sum;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a);
		sum=0.0;
		for(j=1;j<=a;j++)
			sum+=log10(j);  //  取对数
		printf("%d\n",(int)sum+1); // 取整后加一
	}
	return 0;
}</strong></span>