题解 | #数组分组#

数组分组

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);
    }
}

全部评论

相关推荐

11-23 15:33
已编辑
门头沟学院 Java
CUTMR:换账号试试重启推荐算法,我换账号之后回复率还不错,约莫有个20%左右的消息回复率,前几页、主动招呼的HR也开始符合我期望薪资,此前的大号从招呼、回复、前几页的岗位薪资在涨幅30%+以上 用着用着聊着聊着就变成-20%,而且我开通会员之后直接0面试
点赞 评论 收藏
分享
11-12 14:30
已编辑
广东科技学院 前端工程师
迷茫的小刺猬在迎接o...:前端岗位越来越少了,中小厂也更倾向全栈了,更不需要初级或者实习。可能就大厂才会有一些岗位,但是很看学历。
实习,投递多份简历没人回...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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