标签:
1 2 3
1 2 6
import java.math.BigDecimal; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = 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; } }
#include<stdio.h> #include<string.h> #define maxn 50000 int 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;//进位 for(j=0; j<count; j++)//阶乘位数 { 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; }
#include<stdio.h> #include<string.h> #define maxn 50000 int 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++)//长度,每次都到最大,导致超时!!!! { 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! (大数阶乘,还是Java大法好,C也不能错过!!!)
标签:
原文地址:http://blog.csdn.net/hurmishine/article/details/51388306