题解 | #数组分组#
数组分组
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 five = 0;
int three = 0 ;
int[] arr = new int [a];
for (int i = 0 ; i < a ; i++) {
int b = in.nextInt();
if (b % 5 == 0 && b % 3 == 0) {
arr[i] = b;
} else if (b % 5 == 0) {
five += b;
} else if (b % 3 == 0) {
three += b;
} else {
arr[i] = b;
}
}
System.out.println(getRes(five, three, arr, 0));
}
}
public static boolean getRes(int five, int three, int [] arr, int i) {
if (i == arr.length && five == three) {
return true;
} else if (i == arr.length && five != three ) {
return false
;
}
return getRes(five + arr[i], three, arr, i + 1) ||
getRes(five, three + arr[i], arr, i + 1);
}
}
查看19道真题和解析
