hdu 1018 Big Number(公式求阶乘位数)

Big Number

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


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 1
题意:给你一个数a,求a!的位数,在数学中有求一个数阶乘位数公式
分析:
    123456=1.23456*10^5;
    log10(123456)=5.09151;
    log10(1.23456*10^5)=log10(1.23456)+log10(10^5)=0.09151+5;
    故int(log10(n))+1 就是n的位数
5! log10(5!)+1=log10(1*2*3*4*5)+1=log10(1)+log10(2)......
注意log 返回的值是double 型
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
//10! log10(10!)+1=log10(1)+....log10(10)+1; 
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		int m,i;
		double sum=0;
		cin>>m;
		for(i=1;i<=m;i++)
		{
			sum+=log10(i);//返回double 型 
		}
		cout<<(int)sum+1<<endl;
	}
}


posted @ 2017-02-12 15:36  X_na  阅读(138)  评论(0)    收藏  举报