求解大数据的阶乘的值的显示

来源:互联网 发布:深圳多迪网络靠谱吗 编辑:程序博客网 时间:2024/05/17 02:18

近期,在学习《算法竞赛入门经典》(刘汝佳著)时,看到了这个例程感觉很适合大家学习下。

(具体分析过程参见原书)

#include <cstdio>#include <cstring>const int maxn=3000;int f[maxn];int main(){int i, j, n;scanf("%d", &n);memset(f, 0, sizeof(f));f[0] = 1;for(i=2; i<=n; i++){int c=0;for(j=0; j<maxn; j++){int s = f[j]*i + c;f[j] = s % 10;c = s / 10;}}for(j=maxn-1; j>=0; j--)if (f[j])break;for (i=j; i>=0; i--)printf("%d", f[i]);return 0;}


0 0
原创粉丝点击