Big Number hdu1018

题目大意:计算阶乘的位数

解法一:对于一个数n,它的位数是(int)log10(n)+1

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double n,m,res;
scanf("%lf",&n);
while(n--)
{
scanf("%lf",&m);
res=0;
for(double i=1;i<=m;i++)
       res+=log10(i);
printf("%lld\n",(long long)res+1); 
}
return 0;
}

解法二:斯特林公式

#include <iostream>
#include <cmath>
using namespace std;
const double pi=acos(-1.0);
const double e = 2.718281828459;
int main()
{
long long n,m,res;
scanf("%lld",&n);
while(n--)
{
scanf("%lld",&m);
   res=log10(2*pi*m)/2+m*(log10(m/e))+1;
   
printf("%lld\n",res); 
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41603898/article/details/80200114