HDU 1042 N! (large number factorial, or Java Dafa Good, C can not miss!!!)

Source: Internet
Author: User

N!Time limit:10000/5000 MS (java/others) Memory limit:262144/262144 K (java/others)
Total submission (s): 73270 Accepted Submission (s): 21210


Problem Descriptiongiven an integer n (0≤n≤10000), your task is to calculate N!

Inputone N in one line, process to the end of file.

Outputfor each n, output N! In one line.

Sample Input
123

Sample Output
126

Authorjgshining (Aurora Dazzle)
original title link: http://acm.hdu.edu.cn/showproblem.php?pid=1042
big number! or Java Dafa good, Rujia Purple book has a factorial example, but time-out!!! See the code for specific reasons!
AC Code 1 (Java)
Import Java.math.bigdecimal;import Java.util.scanner;public class Main {public static void main (string[] args) {Scanner s c = new Scanner (system.in), while (Sc.hasnext ()) {int n = sc.nextint (); System.out.println (Fun (n));}} public static BigDecimal Fun (int n) {BigDecimal s = new BigDecimal (1), for (int i = 1; I <= n; i++) {BigDecimal a = new BigDecimal (i); s = s.multiply (a);} return s;}}

AC Code 2 (c analogue)
#include <stdio.h> #include <string.h> #define MAXN 50000int f[maxn];int Main () {    int i,j,n;    while (scanf ("%d", &n)!=eof)    {        memset (f,0,sizeof (f));        F[0]=1;        int count=1;        for (I=1; i<=n; i++)        {            int c=0;//carry for            (j=0; j<count; j + +)//factorial bits            {                int s=f[j]*i+c;                f[j]=s%10;                C=S/10;            }            while (c)            {                f[count++]=c%10;                c/=10;            }        }        for (j=maxn-1; j>=0; j--)            if (f[j]) break;        for (i=j; i>=0; i--)            printf ("%d", F[i]);        printf ("\ n");    }    return 0;}

Timeout Code:
#include <stdio.h> #include <string.h> #define MAXN 50000int f[maxn];int Main () {    int i,j,n;    while (scanf ("%d", &n)!=eof)    {        memset (f,0,sizeof (f));        F[0]=1;        for (i=2; i<=n; i++)        {            int c=0;            for (j=0; j<maxn; j + +)//length, each time to maximum, resulting in timeouts!!!!            {                int s=f[j]*i+c;                f[j]=s%10;                C=S/10;            }        }        for (j=maxn-1; j>=0; j--)            if (f[j]) break;        for (i=j; i>=0; i--)            printf ("%d", F[i]);        printf ("\ n");    }    return 0;}



HDU 1042 N! (large number factorial, or Java Dafa Good, C can not miss!!!)

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.