题目描述
编写一个函数,传入一个int型数组,返回该数组能否分成两组,使得两组中各元素加起来的和相等,并且,所有5的倍数必须在其中一个组中,所有3的倍数在另一个组中(不包括5的倍数),能满足以上条件,返回true;不满足时返回false。
输入描述:
第一行是数据个数,第二行是输入的数据
输出描述:
返回true或者false
package Face.huawei;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()){
int length = scanner.nextInt();
int[] array = new int[length];
if (length > 0) {
for (int i = 0; i < length; i++) {
array[i] = scanner.nextInt();
}
} else {
System.out.println(false);
}
System.out.println(Main.into(length,array));
}
}
private static boolean into(int length,int[] array) {
return Main.fenzu(array);
}
private static boolean fenzu(int[] array) {
int five = 0;
int three = 0;
List lastList = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
if (array[i] % 5 == 0) {
five = five + array[i];
} else if (array[i] % 3 == 0) {
three = three + array[i];
} else {
lastList.add(array[i]);
}
}
int difference = five - three;
List defList = new ArrayList<>();
defList.add(0);
for (Integer integer : lastList) {
defList = Main.getArray(defList, integer);
}
for (Integer sum:defList){
if (sum==difference ||sum==difference*(-1)){
return true;
}
}
return false;
}
private static List getArray(List defList, int integer) {
List endList = new ArrayList<>();
for (Integer inte : defList) {
endList.add(inte + integer);
endList.add(inte - integer);
}
return endList;
}
}