HDOJ1042 N! (Big Data factorial)

Source: Internet
Author: User

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 ;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.