Big Number
- 题目链接
Big Number
- 题目大意
意思就是说有n个输入,每个输入有一个数x,现在让你求x!总共有多少位。
- 题解
这个题暴力肯定是不行的,考虑数学方法。
假设一个数x,现有10^(a-1)<=x<10^a . 那么这个数的位数就是a。
现在我们对两边取对数,我们得到a-1<=lg(x) < a,也就是两个式子:
a<=lg(x)+1
a>log(x)
于是我们可以求出a=(int)lg(x)+1。
用上面那个式子就已经可以解出这道题了,不过关于n!还有一个公式叫斯特林公式,可以近似的求出n!,我们用斯特林公式就可以更快的得到结果。
- 代码(斯特林公式版)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const double pi=3.1415926;
const double e=2.71828182;
double ans;
int n,x;
int main()
{
scanf("%d",&n);
while (n--)
{
scanf("%d",&x);
ans=1.0/2*log10(2*pi*x)+x*(log10(x)-log10(e));
printf("%d\n",(int)ans+1);
}
return 0;
}