1007. N!的最高位

51174500037

斯特林公式由于精度损失不太适用于
n=0,1,2,3,7,8
尤其是7 是5040,稍微不小心就掉到了4开头

include

include

include

using namespace std;//10^(lgn!)=10^{(lgn!)}10^[lgn!]
const long double es = 2.718281828459;
const long double pi = 3.1415926535898;
const long double lll = 0.00000001;
int main()//斯特林公式d=lg(n!)=(n
log10(n/e)+1+0.5log10(2pin));
{
long double n;
while(cin>>n)
{
if(n==0||n==1)cout<<1<<endl;
else if(n==2)cout<<2<<endl;
else if(n==3)cout<<6<<endl;
else if(n==7)cout<<5<<endl;
else if(n==8)cout<<4<<endl;
else
{
long double pows = 0.0;
pows = ((n+0.5)
log10(n)-nlog10(es)+0.5log10(2)+0.5*log10(pi));
//cout<<pows<<endl;
pows = pows-(int)pows;
printf(“%.d\n”,(int)pow(10.0,pows));
}

}

return 0;

}

51174500037

是的,所以前面处理要小心,不过还好,其实到了8以后就没有损失太多了

10175102128

n趋于正无穷时, 两者才是等价的无穷大.

CatFlowers

斯特林公式对较小的数有出入,所以较小的数可以换个写法

include

include

const long double es = 2.718281828459;
const long double pi = 3.1415926535898;
int main()
{
int n,i;
while(~scanf(“%d”,&n))
{
double a=0.0;
if(n==0)a=0;
else if(n<10)
for(i=1;i<=n;i++)
a+=log10(i);

else
a = ((n+0.5)log10(n)-nlog10(es)+0.5log10(2)+0.5log10(pi));
a-=(int)a;
printf(“%d\n”,(int)pow(10.0,a));
}
return 0;
}

你当前正在回复 博客/题目
存在问题!