异想天开

What's the true meaning of light, Could you tell me why

大数阶乘

日期:2015-05-08 14:37:15
  
最后更新日期:2015-05-12 22:16:50
NYOJ
http://acm.nyist.net/JudgeOnline/problem.php?pid=28
描述
我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?
输入
输入一个整数m(0输出
输出m的阶乘,并在输出结束之后输入一个换行符
样例输入
50
样例输出
30414093201713378043612608166064768844377641568960512000000000000
解析:
大数的阶乘。利用一个数组存储大数的个位,百位...,然后用因子相乘,进位。假设当前位为current,因子为x,进位为carry,那么下一位的计算如下:
current[i+1]=(x*current[i]+carry[i])%10;
carry[i+1]=(current[i]*x+carry[i])/10;

[code lang="cpp"]
#include<stdio.h>

int result[25001];

int sovle()
{
int n,i,j,len,carry,current;
scanf("%d",&n);
result[0]=1;
len=1;
for (i=2;i<=n;i++){
carry=0;
for (j=0;j<len;j++){
current=result[j];
result[j]=(i*current+carry)%10;
carry=(current*i+carry)/10;
}
while (carry>0){
current=carry;
carry=current/10;
result[len]=current%10;
len++;
}
}

for (i=len-1;i>=0;--i){
printf("%d",result[i]);
}
printf("\n");
return 0;
}

int main()
{
sovle();
return 0;
}
[/code]