注意每个阶乘只能用一次

Sum of Factorials

https://www.nowcoder.com/practice/42cb1c7f7344466b8cb1710cb12d06b1

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int[] factorials = new int[10];
        factorials[0] = 1;
        for (int i = 1; i < 10; i++) {
            factorials[i] = factorials[i - 1] * i;
        }
        Scanner sc = new Scanner(System.in);
        Label:
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            int i = 9;
            while (i >= 0 && factorials[i] > n) --i;
            while (i >= 0) {
                if (n >= factorials[i]) {
                    n -= factorials[i];
                }
                if (n == 0) {
                    System.out.println("YES");
                    continue Label;
                }
                --i;
            }
            System.out.println("NO");
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 15:58
投个小米提前批试试水,先投一个岗位看看形势,不行就再沉淀一下投第二个岗位,莫辜负
Java抽象带篮子:我嘞个骚刚,已经开始研发6g了吗
投递小米集团等公司7个岗位
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务