HDU 1018 Big Number(斯特林公式)

版权声明:本文为博主原创文章,欢迎转载。如有问题,欢迎指正。 https://blog.csdn.net/weixin_42172261/article/details/89429766

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 ≤ 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

斯特林公式:用于求n阶乘的近似值
在这里插入图片描述
两边同时取对数:lnN!=NlnN-N+0.5ln(2N*pi)
要想求有多少位,将他换成以10为底,求出数向下取整再加1.
利用换底公式得 lnN!/ln10=log10N!

#include <iostream>
#include <cstdio>
#include <cmath>
#define PI acos(-1.0)
using namespace std;

int solve(int n)
{
	return (int)((n*log(n)-n+(log(2*PI*n))/2)/log(10))+1;
}
int main()
{
	int t, n;
	scanf("%d", &t);
	while (t--){
		scanf("%d", &n);
		printf("%d\n", solve(n));
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_42172261/article/details/89429766