#include<iostream> #include<cmath> using namespace std; int get(int m){ int pass = 0; //進位 int num[100]; //存儲位數 num[0] = 1; //一個數的第幾位 int count = 0; //記錄進位的位數 int s = 0; int number = 1; //記錄數目的長度 for(int i=1;i<=m;i++){ //階乘 for(int j=0;j<number;j++){ //數組須要操做有數的位置 s = pass; pass = (num[j]*i + pass)/10; //肯定進位 num[j] = (num[j]*i + s)%10; //肯定後一位 } s = pass; while(pass > 0) //輸出進位的位數 多是1位數 多是2位數,多是3位數 。。。。 { count++; pass = pass/10; } while(count){ num[number] = s%10; //存儲進位的值 number++; s = s/10; count--; } pass = 0; } return number; //返回長度 } int main(){ int n; cin>>n; //輸入須要計算數目 while(n--){ int m; cin>>m; cout<<get(m)<<endl; //獲得每一個樣例結果 } }
後面發現對於N!來講是比較快的了,可是對於n = 1000000時仍然會超時,因而乎決定不能這麼直接php
解題思路:ios
#include<iostream> #include<cmath> using namespace std; double get(int m){ double cnt = 0; for(int i=2;i<=m;i++){ cnt += log10(i); } return cnt; } int main(){ int n; cin>>n; //輸入須要計算數目 while(n--){ int m; cin>>m; cout<<1+(int)get(m)<<endl; //獲得每一個樣例結果 } }
方法2:git
#include<iostream> #include<cmath> using namespace std; #define PI 3.141592653 #define ln10 log(10.0) double get(int N){ double cnt = 0; cnt = ceil((N*log(double(N))-N+0.5*log(2.0*N*PI))/ln10); return cnt; } int main(){ int n; cin>>n; //輸入須要計算數目 while(n--){ int m; cin>>m; cout<<(int)get(m)<<endl; //獲得每一個樣例結果 } }
關於log10()能夠參考 : https://msdn.microsoft.com/zh-cn/library/t63833dz.aspx;數組
關於ceil()函數,向上取整 ,能夠參考: https://baike.baidu.com/item/ceil/10931457?fr=aladdinapp
仍是數學公式好,死算的極可能GG函數