大数的阶乘
2015-11-01 19:45
369 查看
#include<stdio.h> #include<string.h> /* 一个数组元素表示4 个十进制位,即数组是万进制的*/ #define BIGINT_RADIX 10000 #define RADIX_LEN 4 /* 10000! 有35660 位*/ #define MAX_LEN (35660/RADIX_LEN+1) /* 整数的最大位数*/ int x[MAX_LEN + 1]; /** * @brief 打印大整数. * @param[in] x 大整数,用数组表示,低位在低地址 * @param[in] n 数组x 的长度 * @return 无 */ void bigint_print(const int x[], const int n) { int i; int start_output = 0; /* 用于跳过前导0 */ for (i = n - 1; i >= 0; --i) { if (start_output) { /* 如果多余的0 已经都跳过,则输出*/ printf("%04d", x[i]); } else if (x[i] > 0) { printf("%d", x[i]); start_output = 1; /* 碰到第一个非0 的值,就说明多余的0 已经都跳过*/ } } if(!start_output) printf("0"); /* 当x 全为0 时*/ } /** * @brief 大整数乘法, x = x*y. * @param[inout] x x * @param[in] y y * @return 无 */ void bigint_mul(int x[], const int y) { int i; int c = 0; /* 保存进位*/ for (i = 0; i < MAX_LEN; i++) { /* 用y,去乘以x 的各位*/ const int tmp = x[i] * y + c; x[i] = tmp % BIGINT_RADIX; c = tmp / BIGINT_RADIX; } } /** * @brief 计算n 的阶乘 * @param[in] n * @param[out] x 存放结果 * @return 无 */ void bigint_factorial(int n, int x[]) { int i; memset(x, 0, sizeof(int) * (MAX_LEN + 1)); x[0] = 1; for (i = 2; i <= n; i++) { bigint_mul(x, i); } } int main() { int n; while (scanf("%d", &n) != EOF) {//计算10000以内数字的阶乘 bigint_factorial(n, x); bigint_print(x, MAX_LEN + 1); printf("\n"); } return 0; }
相关文章推荐
- Linux操作系统及其常用命令
- spring复杂对象注入四种方式
- Animation动画视图
- Python学习笔记(二)
- Apache FlexJS 0.0.1 和 Apache Flex FalconJX 0.0.1发布
- 【SCOI2005】【BZOJ1088】扫雷Mine
- 最长上升子序列O(n^2)
- 20135333苏正生期中总结
- 浅窥ArcGIS Data Store之两斑
- iOS UIWebView
- 经典数据结构和算法回顾
- 进程的虚拟地址空间
- 系统提示移动硬盘要格式化才能使用
- 安卓支付之ping++开发
- CodeForces 400A(Inna and Choose Options)
- 参数估计
- hdu4784Dinner Coming Soon bfs搜索
- c++工厂模式(Factory method)
- 怎样用 16.7 个小时做 40 小时的工作
- UIVisualEffectView