题解 | #数组分组#

数组分组

https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int sum3 = 0, sum5 = 0, sum = 0;
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                int temp = in.nextInt();
                if (temp % 5 == 0) {
                    sum5 += temp;
                } else if (temp % 3 == 0) {
                    sum3 += temp;
                } else {
                    list.add(temp);
                }
                sum += temp;
            }
            if (sum % 2 != 0) {
                System.out.println(false);
                return;
            } else {
                int target = sum / 2 - sum3;
                System.out.println(helper(list, target, 0));
            }

        }
    }

    public static boolean helper(List<Integer> list, int target, int index) {
        if (list.size() == index) return target == 0;
        return helper(list, target - list.get(index), index + 1) ||
               helper(list, target, index + 1);
    }
}

全部评论

相关推荐

12-18 19:36
已编辑
门头沟学院 Java
程序员牛肉:可以的,简历没毛病了。 虽然还是偏向同质化,不过学历不错。后续我觉得重心放到刷实习+摆脱同质化问题上
实习简历求拷打
点赞 评论 收藏
分享
算法冲刺中:kpi面加一,面完完全没动静,感谢信都没有
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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