题解 | #数组分组#
数组分组
https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { ArrayList<Integer> list = new ArrayList<>(); int n = sc.nextInt(); int val = 0; int sum = 0; for (int i = 0; i < n; i++) { int k = sc.nextInt(); sum = sum + k; if (k % 5 == 0) { val = val + k; } else if (k % 3 != 0) { list.add(k); } } if (sum % 2 != 0) { System.out.println(false); } else { int num = sum / 2 - val; System.out.println(function(list, num)); } } } public static boolean function(List<Integer> list, int n) { if (n == 0) { return true; } if (list.size() == 0) { return false; } for (int i = 0; i < list.size(); i++) { if (list.get(i) == n) { return true; } } for (int i = 0; i < list.size(); i++) { int k = list.get(i); List<Integer> tempList = new ArrayList<>(list); tempList.remove((Integer)k); if (function(tempList, n - k)) { return true; } } return false; } }