HDU 1042 (大数)

来源:互联网 发布:app制作编程 编辑:程序博客网 时间:2024/05/18 02:14

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
123
 

Sample Output
126
C++版:
#include "stdio.h"#include "string.h"#define N 100000int f[N];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);}}}



0 0
原创粉丝点击