Java实现 洛谷 P1009 阶乘之和

Java实现 洛谷 P1009 阶乘之和_第1张图片

import java.util.Scanner;


public class 阶乘之和 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int [] sum = new int [101];
		int [] num = new int [101];
		num[1]=1;
		for (int i = 1; i <=n; i++) {
			int a=0,b,c;
			for (int j = 1; j < 101; j++) {
				num[j]=num[j]*i+a;
				a=num[j]/10;
				num[j]=num[j]%10;				
			}
			a=0;
			for (int j = 0; j < 101; j++) {
				sum[j]=sum[j]+num[j]+a;
				a=sum[j]/10;
				sum[j]%=10;
			}
		}
		boolean a = true;
		for (int i = num.length-1; i >0; i--) {
			if(a&&sum[i]==0){
				continue;
			}
			else {
				a=false;
			}
			System.out.print(sum[i]);
		}
	}

}

PS:

这里有个大佬把样例给破了,小编实属佩服,附下:

if(n==0)cout<<"1"<

你可能感兴趣的:(洛谷)