HDU Big Number

题目

Total Submission(s) : 23 Accepted Submission(s) : 4
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 <= m <= 10^7 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

一、分析

转自:https://blog.csdn.net/SteelBasalt/article/details/48288649
后来查了一下才发现,数字的位数可以通过log’10来判断,比如21,log10(21)=1.几,那么数字的位数就是2
这里阶乘还可以很好的利用起来log化乘为加的性质,即

log(n!)=log(12345*…*10)=log(1)+log(2)+log(3)+…+log(n)

用一个循环,再取整加一就可以了

二、代码

代码如下(示例):

#include<iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;
int k;
double x,y;
int main()
{
    
    
	int n,m,i,j;
    scanf("%d",&n);
		while(n--)
		{
    
        x=0;
			cin>>m;
			for(i=1;i<=m;i++)
			   {
    
    
			    x=x+log10(i);   
			   }
			   k=x+1;
            cout<<k<<endl;
		}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wenrenfudi/article/details/113486006