二、stl ,模拟,贪心等 [Cloned] K - Big Number

原题:

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. 

题意:

给出一个大数,要求出这个大数的阶乘的位数。

题解:

显然要直接求阶乘是不太现实的,不过用数组来储存然后计算阶乘应该也不是不可以。但是因为是要求位数,求一个数的位数的公式为Len=(int)log10(M)+1,而阶乘的log运算可以用将乘法转换为加法,所以只要计算从1到n的位数加和然后加一可以求解。

代码:AC

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
int main()
{
	int N;
	cin>>N;
	while(N--)
	{
		long long  n;
		cin>>n;
		int i=1;
		double sum=0;
		for(i=1;i<=n;i++)
		{
			sum+=log10((double)i);
		}
		cout<<(int)sum+1<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/npuyan/article/details/81395617
今日推荐