题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042
N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54119 Accepted Submission(s): 15343
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
思路:将结果分成四位存在数组里面,把进位关系处理好就OK了= =,然后学会了一个输出的格式;
#include<stdio.h>
int main()
{
int i,j,m,n;
while(scanf("%d",&n)!=EOF)
{
// freopen("out.txt","w",stdout);
if(n<0)
continue;
int a[10005]= {0};
m=0;
a[0]=1;
for(i=1; i<=n; i++)
for(j=0; j<=m; j++)
{
a[j]=a[j]*i;
if(j>0&&a[j-1]>=10000) //进位处理
{
a[j]=a[j]+a[j-1]/10000; //满10000进位
a[j-1]=a[j-1]%10000;
}
if(a[m]>=10000)
m++;
}
printf("%d",a[m]);
for(i=m-1; i>=0; i--)
printf("%04d",a[i]); //按四位输出,不足补0
printf("\n");
}
return 0;
}