题目描述
输入一个正整数N,输出N的阶乘。
输入
正整数N(0<=N<=1000)
输出
输入可能包括多组数据,对于每一组输入数据,输出N的阶乘
样例输入 Copy
0 4 7
样例输出 Copy
1 24 5040
注意:n会到1000,阶乘远超long long, 得用大整数
#include <iostream>
#include <string.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct bign{
int d[100000], len;
bign(){
memset(d, 0, sizeof(d));
len = 0;
}
};
void output(bign a){
for(int i = a.len - 1; i >= 0; i--){
printf("%d", a.d[i]);
}
printf("\n");
}
bign mul(bign a, int b){
bign c;
int carry = 0;
for(int i = 0; i < a.len; i++){
int temp = a.d[i] * b + carry;
c.d[c.len++] = temp % 10;
carry = temp / 10;
}
while(carry){
c.d[c.len++] = carry % 10;
carry /= 10;
}
return c;
}
int main(int argc, char** argv) {
int n;
while(cin >> n){
if(!n){//0单独处理
printf("1\n");
continue;
}
bign a;
a.len = 1;
a.d[0] = 1;
for(int i = 2; i <= n; i++){
a = mul(a, i);
}
output(a);
}
return 0;
}