我真是个大聪明

P1009 [NOIP1998 普及组] 阶乘之和

guluguluta @ 2024-08-05 11:58:44

import java.util.Scanner;
public class Main{
    int[] num = new int[100], ans = new int[100];
    Main(){
        ans[0] = 1;
        num[0] = 1;
        num[1] = 1;
    }//num[0]为数字长度
    public void cf(int num2[]){
        int[] num1 = new int[100];
        for(int i=0; i<=num[0]; i++){
            num1[i] = num[i];
        }
        for(int i=1; i<=num[0]; i++){
            num[i] = 0;
        }
        num1[0] = num[0];
        for(int i=1; i<=num2[0]; i++){
            for(int j=1; j<=num1[0]; j++){
                num[j+i-1] += num1[j] * num2[i];
            }
        }
    }
    public void tidynum(){
        int i=1;
        while(!(num[i] == 0 & i > num[0])){
            num[i+1] += num[i] / 10;
            num[i] %= 10;
            i++;
        }
        num[0] = i-1;
    }
    public void tidyans(){
        int i=1;
        while(!(ans[i] == 0 & i > ans[0])){
            ans[i+1] += ans[i] / 10;
            ans[i] %= 10;
            i++;
        }
        ans[0] = i-1;
    }
    public void jf(){
        for(int i=1; i<=(num[0] > ans[0] ? num[0] : ans[0]); i++){
            ans[i] += num[i];
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Main noip = new Main();
        for(int i=1; i<=n; i++){
            int[] x = new int[10];
            int a = i;
            while(a != 0){
                x[0] ++;
                x[x[0]] = a%10;
                a /= 10;
            }
            noip.cf(x);
            noip.tidynum();
            noip.jf();
            noip.tidyans();
        }
        for(int i=noip.ans[0]; i>=1; i--){
            System.out.print(noip.ans[i]);
        }
    }
}

|