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.
InputInput 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 ≤ 10 7 on each line.
OutputThe 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
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n,a,i;
double len;
cin>>n;
while(n>0)
{
cin>>a;
len=0;
for(i=1;i<=a;i++)
{
len+=log10(i);
}
cout<<(int)len+1<<endl;
n--;
}
return 0;
}
题解:一个自然数n的位数用lg(n)+1;
n!的位数即lg(n!)+1=lg(n*(n-1)*(n-2)....*3*2*1)+1=(lg(n)+lg(n-1)+...+lg(3)+lg(2)+0)+1。
本文介绍了一个计算特定整数阶乘位数的C++程序实现方法,通过求和对数的方式避免了直接计算阶乘导致的大数运算问题。
705

被折叠的 条评论
为什么被折叠?



