题解 | #数组分组#
数组分组
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);
}
}

