Java AC code:
It took only 1000 MS to use Java !!! (The question is limited to Ms)
import java.util.Scanner;import java.math.BigInteger;public class Hdoj1042 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubScanner scanner=new Scanner(System.in); int n;BigInteger m;while(scanner.hasNextBigInteger()){n=scanner.nextInt();m=BigInteger.ONE;for(int i=2;i<=n;i++)m=m.multiply(BigInteger.valueOf(i));System.out.println(m);}}}
C ++ code:
This C ++ code is adapted from a factorial algorithm in the White Paper of instructor Liu lujia. I really admire Mr. Liu, but it took 3765 MS to write in C ++.
// Hdoj1_2.cpp # include <iostream> # include <string> using namespace std; const int MAXN = 36000; // calculate 10000! Int a [MAXN]; int main () {int n, I, j; while (scanf ("% d", & n )! = EOF) {memset (a, 0, sizeof (a); a [0] = 1; for (I = 2; I <= n; I ++) {int c = 0; for (j = 0; j <MAXN; j ++) {// This section is very skillful, you can understand int s = a [j] * I + c; a [j] = s % 10; c = s/10 ;}} for (j = MAXN-1; j> = 0; j --) if (a [j]) break; for (I = j; I> = 0; I --) printf ("% d", a [I]); printf ("\ n");} return 0 ;}