使用递归将某个数组按特定条件划分为2组

时间:2018-06-03 17:39:44

标签: java arrays recursion

给定任务:"编写名为' mysplit'获取一个int数组并调用一个 递归方法名为' MySplit'。函数MySplit需要检查是否 当前数组可以分为两组,遵循以下条件:

*每组的总和必须相等。

*每个数字的出现只在每个组中出现一次(此子句旨在防止用户添加/复制数字,以便在2组=第一个子句之间取得相等)。

*所有可以除以5的数字必须在同一组中。

*所有可以除以3(而不是5)的数字必须在第二组中。

我已经编写了这段代码,但是达到了死胡同。我未能完成任务的前两个条款,并且无法想出将其插入到我的代码中的方法。

这是我写的代码:

input_data_1

*编辑: **范例

mySplit([1,1])==>真

mySplit([1,1,1])==>假

mySplit([2,4,2])==>真

mySplit([5,21,8,15,7])==>真

mySplit([15,10,5])==>假

mySplit([15,8,7])==> true

2 个答案:

答案 0 :(得分:0)

注意评论。如果需要进一步澄清,请不要犹豫:

public static void main(String[] args) {

    System.out.println(mySplit(1,1));           //true
    System.out.println(mySplit(1,1,1));         //false
    System.out.println(mySplit(2,4,2));         //true
    System.out.println(mySplit(5,21,8,15,7));   //true
    System.out.println(mySplit(15,0,5));        //false
    System.out.println(mySplit(15,8,7));        //true
}

public static boolean mySplit(int...nums)
{
    List<Integer> groupA = new ArrayList<>(); //initialized by all numbers divided by 5
    List<Integer> groupB = new ArrayList<>(); //initialized by all other numbers divided by 3
    List<Integer> groupC = new ArrayList<>(); //initialized by all other numbers

    for(int number : nums) {
        if((number % 5) == 0 ) {
            groupA.add(number);
        }else if ((number % 3) == 0 ) {
            groupB.add(number);
        }else {
            groupC.add(number);
        }
    }

    return mySplit(groupA, groupB, groupC);
}

private static boolean mySplit(List<Integer> groupA, List<Integer> groupB, List<Integer> groupC) {

    if(groupC.size() == 0 ) { //no more numbers to add
        return  sumList(groupA) == sumList(groupB);
    }

    int next = groupC.get(0);
    groupC.remove(0);

    //make a new group A which includes next 
    List<Integer> newGroupA = new ArrayList<>(groupA);
    newGroupA.add(next);

    //make a new group B which includes next 
    List<Integer> newGroupB = new ArrayList<>(groupB);
    newGroupB.add(next);

    //check with new A and B. Make a defensive copy of groupC 
    //to prevent changes in groupC 
    if( mySplit(newGroupA, groupB, new ArrayList(groupC))) {

        return true;
    //check with A and new B
    }else if( mySplit(groupA, newGroupB, new ArrayList(groupC))) {

        return true;
    }

    return false; //equality not found 
}

//sum a list 
private static int sumList(List<Integer> list) {

    /* a pre java 8 version
        int sum = 0;
        for( int i : list ) {  sum += i;}
        return sum;
    */
    return list.stream().mapToInt(Integer::intValue).sum();
}

您可以将mySplit(int...nums)的签名更改为mySplit(int[] nums)并通过mySplit(new int[]{2,4,2})

调用它

答案 1 :(得分:0)

这是我想出的:

public static void main(String[] args)
{
    System.out.println(mySplit(new int[] { 1, 1 })); // ==>true

    System.out.println(mySplit(new int[] { 1, 1, 1 })); // ==>false

    System.out.println(mySplit(new int[] { 2, 4, 2 })); // ==>true

    System.out.println(mySplit(new int[] { 5, 21, 8, 15, 7 })); // ==>true

    System.out.println(mySplit(new int[] { 15, 10, 5 })); // ==>false

    System.out.println(mySplit(new int[] { 15, 8, 7 })); // ==>true
}

public static boolean mySplit(int[] nums)
{
    if (nums.length == 0)
        return false;

    return mySplit(nums, 0, 0, 0);
}

private static boolean mySplit(int[] nums, int i, int groupA, int groupB)
{
    if (i == nums.length)
        return groupA == groupB; // At the end, check if the sum of each group is equal.

    if (nums[i] % 5 == 0)
        return mySplit(nums, i + 1, groupA + nums[i], groupB); // All the numbers that can be divided by 5 must be in the same group (Group A).

    if (nums[i] % 3 == 0 && nums[i] % 5 != 0)
        return mySplit(nums, i + 1, groupA, groupB + nums[i]); // All the numbers that can be divided by 3 (and not by 5) must be in the second group (Group B).

    return mySplit(nums, i + 1, groupA + nums[i], groupB) || mySplit(nums, i + 1, groupA, groupB + nums[i]);
}