高精度计算--大数处理--计算阶乘结果的位数

时间:2021-11-20 03:35:59

Description

In manyapplications very large integers numbers are required. Some of theseapplications are using keys for secure transmission of data, encryption, etc.In this problem you are given a number, you have to determine the number ofdigits in the factorial of the number. 

Input

Input consistsof 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. 

Output

The outputcontains the number of digits in the factorial of the integers appearing in theinput. 

Sample Input

 2

10

20

Sample Output

 7

19

 

 题目大意:计算阶乘结果的位数,如10! = 3 628 800,总共七位。

思想:取对数,10!=1*2*3*4..... 则log10(10!)=log10(1)+log10(2)+log10(3)....

位数就是这些和取整再加1


AC代码:

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<string>
#include<map>
#include<set>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,i;
cin>>n;
double s=0;
for(i=1;i<=n;i++)
s=s+log10(i);
printf("%d\n",(int)s+1);
}
}