HDU 1042 N! 阶乘 高精度(java)

时间:2022-10-03 03:38:07

题目链接:HDOJ 1042\



题意:求最大到10000的阶乘。



题解:可想而知,n到10000时这个答案太大,所以必须要高精度;


AC code:(java)

//package adruill;
import java.util.Scanner;
public class Main {
    public static void main(String[] agrs){
        Scanner in = new Scanner(System.in);    
        int maxn = 40005;//10000的阶乘也不超40000位预开数组
        int a[] = new int[maxn];
        while(in.hasNext()){
            for(int i = 0; i < maxn; i++)
                a[i] = 0;
        a[0] = 1;						
        int t = in.nextInt();
        for(int i = 2; i <= t; i++){		//高精度
            for(int j = 0; j < maxn; j++)
            {
                if(a[j] == 0)
                    continue;
                a[j] *= i;
            }
            for(int j = 0; j < maxn - 1; j++)
            {
                if(a[j] == 0)
                    continue;
                a[j + 1] += a[j] / 10;
                a[j] %= 10;
            }
        }
        int k = maxn - 1;
        while(a[k] == 0)				//去前导0
            k--;
        for(int i = k; i >= 0; i--)
            System.out.print(a[i]);
        System.out.println();
        }
        in.close();
    }
}