您的位置:首页 > 其它

HDU 1042 N!(大数阶乘)

2015-07-24 18:10 281 查看

                  N!

[align=left]Problem Description[/align]
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!

[align=left]Input[/align]
One N in one line, process to the end of file.

[align=left]Output[/align]
For each N, output N! in one line.

[align=left]Sample Input[/align]

1
2
3

[align=left]Sample Output[/align]

1
2
6

代码:

#include<cstdio>
using namespace std;

int main()
{
int i,n,digit,temp,carry,j;
int a[40005];
while(scanf("%d",&n)!=EOF)
{
a[0]=1;
digit=0;
for(i=2;i<=n;i++)
{
for(carry=0,j=0;j<=digit;j++)
{
temp=a[j]*i+carry;
a[j]=temp%10;
carry=temp/10;
}
while(carry)
{
a[++digit]=carry%10;
carry/=10;
}
}
for(i=digit;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: