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 19
题意:给你一个数,让你求出N!由多少位数构成
思路:
1.暴力,N的阶乖的位数等于log10(N!)=log10(1)+…..log10(N);
2.Stirling公式:
故log10(n!) = log(n!) / log(10) = ( n*log(n) - n + 0.5*log(2*π*n))/log(n);
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int n, t, i, ans;
double tt;
cin >> t;
while(t--)
{
ans = 0;
cin >> n;
tt = 0;
for (i = 1; i <= n; i++)
tt += log10(i);
ans = (int)tt + 1;
cout << ans <<endl;
}
return 0;
}