题解 | #数组分组#
数组分组
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.hasNext()) { // 注意 while 处理多个 case int n = in.nextInt(); int[] in_math = new int[n]; List<Integer> list5 = new ArrayList<>(); List<Integer> list3 = new ArrayList<>(); for(int i = 0;i<n;i++){ int gg = in.nextInt(); if(gg%3==0&&gg%5!=0) list3.add(gg); else list5.add(gg); } System.out.println(pan(list5,list3,0)); } } public static boolean pan(List<Integer> list5,List<Integer> list3,int start){ int sum5 = 0; int sum3 = 0; for(int b:list5){ sum5+=b; } for(int b:list3){ sum3+=b; } if(sum5==sum3) return true; boolean panpan = false; for(int i = start;i<list5.size();i++){ if(list5.get(i)%5==0) continue; List<Integer> list5_to = new ArrayList<>(list5); List<Integer> list3_to = new ArrayList<>(list3); list3_to.add(list5.get(i)); list5_to.remove(i); panpan = pan(list5_to,list3_to,i); if(panpan) break; } return panpan; } }