N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 65350 Accepted Submission(s): 18696
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
Author
JGShining(极光炫影)
思路:
i从1開始乘以a[1]并保存到a数组里面,假设a[1]里面的数大于10,对10取余。仅仅要它的个位数。将其与的取商进位。
在此之前一定要对c进行清零操作!然后i再从a中的个位数開始。逐个与a 中的数相乘;(和小学我们列的竖式算乘法一样);就这样以此类推知道i等于n为止!
代码:
#include <stdio.h>
#include <string.h>
int a[110000];
int main()
{
int n,i,j,d,c;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
d=1;
a[1]=1;
c=0;
for(i=1;i<=n;i++)//须要乘的数。
{
for(j=1;j<=d;j++)//j代表中间数值的位数!
{
a[j]=a[j]*i+c;//相当于数学中的列竖式乘法的步骤!
c=0;//每次进过位之后都得清零!
if(a[j]>=10)//大于10的话须要保留个位,其它的数都进到前面!
{
c=a[j]/10;
a[j]=a[j]%10;
}
}
while(c!=0)//假设最高位也比10大。须要将其余数进到下一位。余数又一次赋0!
{
d++;
a[d]=c%10;
c=c/10;
}
}
for(i=d;i>=1;i--)//终于从后往前逐个输出(
printf("%d",a[i]);//这里面的d代表的就是算出来的数的最高位!
)
printf("
");
}
return 0;
}