N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 70861 Accepted Submission(s): 20321
Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
Input
One N in one line, process to the end of file.
Output
For each N, output N! in one line.
Sample Input
1
2
3
Sample Output
1
2
6
求几十位数的阶乘,用数组保存每四位的数字,比如1320 0456 789,会变成list[0]=132,list[1]=456,list[2]=789;输出除了开头的那一组,其余用%04d(不足四位补0)。话说我不看大牛的代码也就只知道输出方式- -|||就当学习了。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<queue> #include<set> #include<map> #include<sstream> #include<algorithm> #include<cmath> #include<cstdlib> using namespace std; int list[40050]={1};//开始是1 int len;//总长度 void jiecheng(int a) { for (int i=0; i<=len; i++) { list[i]*=a; if(list[i-1]>9999&&i>0)//若超过四位数 { list[i]+=list[i-1]/10000;//先进一位 list[i-1]%=10000;//然后前一位取模 } if(list[len]>=10000) len++; } } int main (void) { int n; while (cin>>n) { memset(list,0,sizeof(list));//每次都清空一下,防止干扰 list[0]=1; len=0; for (int i=1; i<=n; i++) { jiecheng(i); } printf("%d",list[len]);//开头的一组正常输出 for (int i=len-1; i>=0; i--) { printf("%04d",list[i]);//不足四位补0 } cout<<endl; } return 0; }