UVA-623大数1000阶乘
发布时间:2020-12-14 03:42:48 所属栏目:大数据 来源:网络整理
导读:循环会比计算浪费更多的时间!!!所以为了减少循环,可以把好几位存到一个数组中!!!
循环会比计算浪费更多的时间!!!所以为了减少循环,可以把好几位存到一个数组中!!!!《算法竞赛入门经典》5.2.2,关于大数的题,按照书上给的方法超时了,计算次数太多了,没有充分利用int,最后又在网上搜了一下,修改代码如下: #include <cstdio> #include <cstring> const int maxn = 1000; int f[maxn]; int main() { #ifdef LOCAL freopen("in","r",stdin); #endif int n; while(scanf("%d",&n) != EOF) { memset(f,sizeof(f)); f[0] = 1; int len = 1; for(int i = 2; i <= n; i++) { int c = 0; for(int j = 0; j < len; j++) { int t = f[j] * i + c; f[j] = t % 1000000; c = t / 1000000; } if(c) f[len++] = c; } printf("%d!n",n); printf("%d",f[len-1]); for(int i = len-2; i >= 0; i--) printf("%06d",f[i]); printf("n"); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |