题解 | #数组分组#
数组分组
http://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
dfs
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int n = sc.nextInt();
int[] a = new int[n];
int left = 0;
int right = 0;
for (int i =0; i< n;i++) {
int tmp = sc.nextInt();
if (tmp % 3 == 0 && tmp % 5 != 0) {
left += tmp;
} else if (tmp % 5 == 0 ){
right+= tmp;
} else {
a[i] = tmp;
}
}
System.out.println(dfs(a, left, right,0));
}
}
public static boolean dfs (int[] a, int left, int right, int index) {
if (index > a.length) {
return false;
}
if (index == a.length) {
return left == right;
}
return dfs(a, left+a[index], right, index +1) || dfs(a, left, right + a[index], index +1);
}
}