习题 92:高精度算阶乘★★
2008-09-28 14:35
211 查看
/*题目描述:
算出n!的完整结果,其中n!=1*2*3*...*n
输入:
多组测试数据,一行一组,每行仅一个数n
其中0<=n<=10000
输出:
输出相应的n!的结果,必须精确到个位
样例输入:
10
20
100
样例输出:
3628800
2432902008176640000
933262154439441526816992388562667004907159682643816214685929
638952175999932299156089414639761565182862536979208272237582
51185210916864000000000000000000000000
其它信息:
最后一个100!的结果由于过长,故拆分成三行,每行60字符,请见谅
难度:Easy
*/
sh_highlightElement(document,code5);
算出n!的完整结果,其中n!=1*2*3*...*n
输入:
多组测试数据,一行一组,每行仅一个数n
其中0<=n<=10000
输出:
输出相应的n!的结果,必须精确到个位
样例输入:
10
20
100
样例输出:
3628800
2432902008176640000
933262154439441526816992388562667004907159682643816214685929
638952175999932299156089414639761565182862536979208272237582
51185210916864000000000000000000000000
其它信息:
最后一个100!的结果由于过长,故拆分成三行,每行60字符,请见谅
难度:Easy
*/
#include <iostream> #include <iomanip> #define MAX_SIZE 10000 using namespace std; //end is the last updated bit int bigMulti(int *result, int n,int size, int end) { int i,j, carry, tmp; carry = 0; for(j = 1;j <= n; j++) { for(i = MAX_SIZE; --i >=end;) { tmp = result[i] * j + carry; result[i] = tmp % 10000; carry = tmp / 10000; } while(carry) { result[--end] = carry % 10000; carry /= 10000; } } return end; } int main(void) { int n, end; int result[MAX_SIZE];//如果这里声明为char会导致速度减慢 while(EOF!=scanf("%d",&n)) { memset(result,0,MAX_SIZE*sizeof(int)); result[MAX_SIZE - 1] = 1;// set result to 1 end = MAX_SIZE - 1; end = bigMulti(result, n,MAX_SIZE, end); cout << result[end++]; for(; end < MAX_SIZE;) { printf("%04d",result[end++]); } cout << endl; //result[j] = 0; //cout << result << endl; } return 0; }
/*result:
78737 Name: "younthu" Problem ID "92" Submit Time: 2008/9/28-13:35 G++: Compile OK Test 1: Accepted Time = 0 ms Test 2: Accepted Time = 0 ms Test 3: Accepted Time = 267 ms -------------------------------- Problem ID 92 Test Result Accepted Total Time 267 ms Total Memory 228 Kb / 2000 Kb Code Length 817 Bytes */
sh_highlightElement(document,code5);
相关文章推荐
- HDU 1042 N!(高精度计算阶乘)
- BNU 0919 日常训练 之 G N! ——高精度 万进制 10000的阶乘
- 高精度运算之阶乘
- POJ 3331 The Idiot of the Year Contest! 高精度阶乘
- ACM HDU 1042 N!(高精度计算阶乘)
- 阶乘、排列,组合的高精度运算
- 阶乘计算(高精度)
- 蓝桥杯-阶乘计算(VIP高精度)
- 蓝桥杯(java)阶乘计算,输入一个正整数n,输出n!的值。(高精度计算,用到数组)
- 蓝桥杯:基础练习 阶乘计算 高精度加法
- 高精度算阶乘
- 算法:高精度阶乘
- 计算阶乘 高精度10000! 5000MS内
- POJ 3331 The Idiot of the Year Contest! 高精度阶乘
- 高精度快速阶乘算法
- 算法篇-高精度-阶乘的精确值
- 高精度阶乘
- 蓝桥杯基础练习 阶乘计算(高精度方法)
- 算法竞赛入门经典 5.2.2高精度阶乘
- 高精度加法,乘法,阶乘