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!!!)