题目大致意思是输入一个T,然后接下来T行中输入n并计算每一行中n的阶乘的位数
解题思路:如果此题不是大数的话可以用对数函数性质:log10(1*2*3*4*5...)=log10(1)+log10(2)...
附上代码,注意sum是double
int main() { ios::sync_with_stdio(false); cin>>T; double sum;//注意double while(T--) { cin>>n; sum=0.0; if(n>1) { for(long long i=1;i<=n;i++) { sum+=log10(i); } } if(n==1) sum=1; cout<<ceil(sum)<<endl;//向上取整函数 } return 0; }
结果就TLE了。。。
正解是用斯特林公式:
附上代码:
const double PI=3.141592654; const double e=2.71828182846; int T,n; int main() { ios::sync_with_stdio(false); cin>>T; while(T--) { cin>>n; int ans=0; if(n>3) ans=(int)(log10((2*PI*n))/2+n*log10((n/e))+1); else ans=1; cout<<ans<<endl;; } return 0; }
斯特林公式更精确方法:https://www.cnblogs.com/stonehat/p/3603267.html
另一种思路的好方法(或许是吧。。。):http://www.cnblogs.com/yuzhaoxin/archive/2011/11/19/2205221.html