题解 | #数组分组#
数组分组
https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
import java.util.Scanner; // 注意类名必须为 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 a = in.nextInt(); int[] arr = new int[a]; int sum3 = 0; int sum5 = 0; int sum = 0; for(int i=0;i<arr.length;i++){ arr[i]= in.nextInt(); if(arr[i]%3==0 &&arr[i]%5!=0){ sum3 += arr[i]; arr[i] = 0; }else if(arr[i]%5==0 ){ sum5 += arr[i]; arr[i] = 0; }else{ sum ++; } } int[] no35 = new int[sum]; for(int i=0,j=0;i<arr.length;i++){ if(arr[i]%3!=0 &&arr[i]%5!=0){ no35[j] = arr[i]; j++; } } System.out.println(dfs(no35,sum3,sum5,0)); } in.close(); } private static boolean dfs(int[] arr,int sum3,int sum5,int index){ if(arr.length<index){ return false; } if(arr.length==index){ return sum3==sum5; } //System.out.println(index+"=================="+sum3+"-----------------------"+sum5); return dfs(arr,sum3+arr[index],sum5,index+1) ||dfs(arr,sum3,sum5+arr[index],index+1); } }