Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 91867 Accepted Submission(s): 27269
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
题解:求N的阶乘,因为数据太大,所以要用万进制。计算9的阶乘,在计算到7的阶乘时,7!为5040,可以用a[0]存储5040,没有产生进位,然后8!=5040*8=40320,如果看成万进制则产生了进位,那么a[0]=320(注意输出时不足4位的要在前边补0),a[1]=4(最高位不用补0),9!=40320*9,那么a[0]*9=320*9=2880,a[1]*9=4*9=36,那么9!=a[1]+a[0]=362880(注意这里的+相当于字符串连接,)
代码如下:
#include <iostream>
#include <iomanip>
using namespace std;
void syetem(int n)
{
int a[10005]={1};//用数组来存储万进制的每一位
int i,j;
int places=0,carry;//总位数,进位的值
for(i=1;i<=n;i++)
{
carry=0;
for(j=0;j<=places;j++)
{
a[j]=a[j]*i+carry;
carry=a[j]/10000;
a[j]%=10000;
}
if(carry!=0)
{
places++;
a[places]=carry;
}
}
printf("%d",a[places]);//最高位原样输出
for(i=places-1;i>=0;i--)
{//其他位小于1000的,最高位补0
if(a[i]>=1000)
{
printf("%d",a[i]);
}else if(a[i]>=100)
{
printf("0%d",a[i]);
}else if(a[i]>=10)
{
printf("00%d",a[i]);
}
else printf("000%d",a[i]);
}printf("\n");
}
int main()
{ int n;
while (~scanf("%d",&n)) {
syetem(n);
}return 0;
}