求n的阶乘(10000)
2016-07-27 10:16
357 查看
题意:如题目
链接:
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1057
思路:用一个数组,数组的每一位存一个8位数,两个for循环,不断从1开始累乘。
代码:
链接:
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1057
思路:用一个数组,数组的每一位存一个8位数,两个for循环,不断从1开始累乘。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100000000; //注意不能超过long long 的表示范围 long long factorial[36000]; int main() { int n; while(scanf("%d",&n) != EOF) { memset(factorial, 0,sizeof factorial); factorial[0] = 1; int len=0; for(int i=1; i<=n ; i++) { long long c=0; for(int j=0; j<=len; j++) { factorial[j] = factorial[j]*i + c; c = factorial[j]/maxn; factorial[j] %= maxn; } if(c > 0) { len++; factorial[len] = c; } } printf("%I64d",factorial[len]); for(int j=len-1; j>=0; j--) printf("%.8I64d",factorial[j]); //注意后面的输出。 printf("\n"); } return 0; }
相关文章推荐
- Redis 数据类型
- HDU 3367
- 正确运行携程DynamicAPK
- 四.swift - string
- AngularJS 指令详细介绍
- iOSpush到下一个页面,去掉tabBar占用页面的大小
- PokemonGo完美度算法!是风姐还是松岛枫?--工地斗天地斗天马座
- <item> tag requires a 'drawable' attribute or child tag defining a drawab
- SQL Server存储机制二
- hdu5753Permutation Bo(排列问题)
- R语言科学计算
- Redis 配置
- Linux 入门之History历史命令
- httpClient(Get)解析JSON
- 正则表达式
- gitlab 8.8.5分支保护和Webhooks、runners、tiggers的作用
- java的常量与变量
- 以字节流读取文件时中文乱码解决(二)
- Python:发送电子邮件
- Linux常用命令大全