斯特林公式由于精度损失不太适用于
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!)=(nlog10(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;
}
是的,所以前面处理要小心,不过还好,其实到了8以后就没有损失太多了
n趋于正无穷时, 两者才是等价的无穷大.