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++版:Java版#include "stdio.h" #include "string.h" #define N 100000 int 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; }
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); } } }