您的位置:首页 > 其它

HDU 1042 (大数)

2014-10-27 16:37 309 查看


N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 56027    Accepted Submission(s): 15904


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
6C++版:
#include "stdio.h"
#include "string.h"
#define N 100000
int f
;
int main()
{int i,j,n,c;
int k=1;
while(scanf("%d",&n)!=EOF)
{memset(f,0,sizeof(f));
f[0]=1;
for(i=2;i<=n;i++)
{   c=0;
for(j=0;j<=k;j++)
{int s=f[j]*i+c;
f[j]=s%10;
c=s/10;
if(k==j && c!=0)
k++;
}

}

for(j=k;j>=0;j--)
if(f[j]) break;
for(i=j;i>=0;i--)
printf("%d",f[i]);
printf("\n");

}
return 0;
}
Java版
import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner cin=new Scanner (new BufferedInputStream(System.in));
int n;
BigInteger a,b;
while(cin.hasNext())
{
n=cin.nextInt();
b=BigInteger.ONE;
a=BigInteger.ONE;
for(int i=2;i<=n;i++)
{
a=a.multiply(BigInteger.valueOf(i));
}
System.out.println(a);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HDU 大数