题目来源:http://codeup.cn/problem.php?cid=100000593&pid=1
题目描述
输入一个正整数N,输出N的阶乘。
输入
正整数N(0<=N<=1000)
输出
输入可能包括多组数据,对于每一组输入数据,输出N的阶乘
样例输入
0 4 7
样例输出
1 24 5040
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
#define nMAX 3001
int lenth; //lenth: the length of list
struct bign{
int d[nMAX],c;
}list[1001]={0};
bign multi(bign b, int n){
for(int j=0;j<b.c;j++){
b.d[j]=b.d[j]*n;
}
for(int j=0;j<b.c;j++){
int t=b.d[j];
b.d[j]=0;
for(int i=0;i<5;i++){
if(t>0){
b.d[j+i]+=t%10;
t/=10;
if(j+i>=b.c)b.c=j+i+1;
}
else break;
}
}
return b;
}
bign get_factorial(int n){
if(n<lenth)return list[n];
for(int i=lenth;i<=n;i++){
list[i]=multi(list[i-1],i);
}
lenth=n+1;
return list[n];
}
int main()
{
int N;
lenth=1;
list[0].c=1;
list[0].d[0]=1;
while(EOF != scanf("%d",&N)){
bign ans;
ans = get_factorial(N);
int t=ans.c;
while(t--)
printf("%d",ans.d[t]);
printf("\n");
}
return 0;
}
博客围绕计算正整数N阶乘的算法展开,给出题目来源,描述输入为正整数N(0<=N<=1000),可能有多组数据,要求输出每组输入数据中N的阶乘,还提及样例输入和输出。
2977

被折叠的 条评论
为什么被折叠?



