HJ93 数组分组 | 题解

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {
    static List<Integer> list = new LinkedList<>();
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            int five = 0, three = 0, sum = 0;
            for (int i = 0; i < n; i++) {
                int num = in.nextInt();
                if (num % 3 == 0 && num % 5 != 0) {
                    three += num;
                } else if (num % 5 == 0) {
                    five += num;
                } else {
                    list.add(num);
                }
            }
            if (helper(three, five, 0)) {
                System.out.println("true");
            } else {
                System.out.println("false");
            }
        }
    }

    public static boolean helper(int three, int five, int i) {
        if (i > list.size()) {
            return false;
        }
        if (i == list.size()) {
            return three == five;
        }
        return helper(three + list.get(i), five, i + 1) || helper(three, five + list.get(i), i + 1);
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务