/*题目描述:
算出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
*/
<script>sh_highlightElement(document,code5);</script>