《算法竞赛入门经典》5.2.2,关于大数的题,按照书上给的方法超时了,计算次数太多了,没有充分利用int,最后又在网上搜了一下,修改代码如下:
View Code
1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 1000; 5 int f[maxn]; 6 7 int main() 8 { 9 #ifdef LOCAL 10 freopen("in", "r", stdin); 11 #endif 12 int n; 13 while(scanf("%d", &n) != EOF) 14 { 15 memset(f, 0, sizeof(f)); 16 f[0] = 1; 17 int len = 1; 18 for(int i = 2; i <= n; i++) 19 { 20 int c = 0; 21 for(int j = 0; j < len; j++) 22 { 23 int t = f[j] * i + c; 24 f[j] = t % 1000000; 25 c = t / 1000000; 26 } 27 if(c) f[len++] = c; 28 } 29 printf("%d!\n", n); 30 printf("%d", f[len-1]); 31 for(int i = len-2; i >= 0; i--) 32 printf("%06d", f[i]); 33 printf("\n"); 34 } 35 return 0; 36 }