描述: | 编写一个函数,传入一个int型数组,返回该数组能否分成两组,使得两组中各元素加起来的和相等,并且,所有5的倍数必须在其中一个组中,所有3的倍数在另一个组中(不包括5的倍数),能满足以上条件,返回true;不满足时返回false。
|
知识点: | 语言基础,字符串,循环,函数,指针,枚举,位运算,结构体,联合体,文件操作,递归 |
题目来源: | 内部整理 |
练习阶段: | 初级 |
运行时间限制: | 10Sec |
内存限制: | 128MByte |
输入: |
输入输入的数据个数 输入一个int型数组
|
输出: | 返回true或者false |
样例输入: | 415-51 |
样例输出: | true |
答案提示: |
|
源代码:
package issplittwointarray;import java.util.ArrayList;import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int arrayLength = sc.nextInt(); int[] intArray = new int[arrayLength]; for(int i = 0; i < intArray.length; i++){ intArray[i] = sc.nextInt(); } boolean isSplitTwoEqualArrays = Main.isSplitTwoEqualArray(intArray); System.out.println(isSplitTwoEqualArrays); } public static boolean isSplitTwoEqualArray(int[] intArray){ int multipleOfFiveSum = 0; int multipleOfThreeSum = 0; ArrayListotherInts = new ArrayList (); for(int i = 0; i < intArray.length; i++){ if(intArray[i] % 5 == 0){ multipleOfFiveSum += intArray[i]; } else if(intArray[i] % 3 == 0){ multipleOfThreeSum += intArray[i]; } else { otherInts.add(intArray[i]); } } int difference = multipleOfFiveSum - multipleOfThreeSum; ArrayList accumulationSum = new ArrayList (); accumulationSum.add(0); for (Integer otherInt : otherInts) { accumulationSum = Main.expressionsAccumulation(accumulationSum, otherInt); } for (Integer sum : accumulationSum) { if((sum == difference) || (sum == difference * (-1))) return true; } return false; } public static ArrayList expressionsAccumulation(ArrayList preSums, int extraInt){ ArrayList accumulationSum = new ArrayList (); for (Integer preSum : preSums) { accumulationSum.add(preSum + extraInt); accumulationSum.add(preSum - extraInt); } return accumulationSum; }}