使用递归按特定条件将数组分为2组 [英] Divide an array into 2 groups by certain a conditions, using recursion

查看:136
本文介绍了使用递归按特定条件将数组分为2组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出任务:编写名为'mysplit'的函数,该函数获取一个int数组,并调用名为'MySplit'的
递归方法。该函数MySplit需要检查
当前数组是否可分为2个符合以下条件的组:



*每个组的总和必须相等。



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



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



我已经编写了这段代码,但是到了穷途末路。我无法完成任务的前2个子句,无法想到



这是我编写的代码:

 公共静态布尔值mySplit(int [ ] nums)
{
return MySplit(nums,0,0,0);
}

公共静态布尔值MySplit(int [] arr,int result_5,int result_3,int指针)// 2组: result_5& 'result_3'并遍历数组
{
if(arr [pointer]%5 == 0)
{
返回MySplit(arr,result_5 + arr [pointer ],result_3,pointer ++);
}
else if(arr [pointer]%3 == 0)
{
return MySplit(arr,result_5,result_3 + arr [pointer],pointer ++);
}

}

*编辑:
**示例:



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



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



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



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



mySplit([15,10 ,5]] ==> false



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

解决方案

请注意注释。如果需要更多说明,请随时询问:

  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<>(); //由所有数字除以5
List< Integer>初始化groupB = new ArrayList<>(); //由所有其他数字除以3
List< Integer>初始化groupC =新的ArrayList<>(); //由所有其他数字初始化

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

返回mySplit(groupA,groupB,groupC);
}

私有静态布尔值mySplit(List< Integer> groupA,List< Integer> groupB,List< Integer> groupC){

if(groupC.size ()== 0){//不再添加
的数字return sumList(groupA)== sumList(groupB);
}

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

//创建一个新的组A,其中包括下一个
List< Integer> newGroupA =新的ArrayList<>(groupA);
newGroupA.add(next);

//创建一个新的组B,其中包括下一个
List< Integer> newGroupB =新的ArrayList<>(groupB);
newGroupB.add(next);

//检查新的A和B.制作一个防御性的groupC
//防止groupC发生变化
if(mySplit(newGroupA,groupB,new ArrayList(groupC ))){{

返回true;
//检查A和新的B
}否则if(mySplit(groupA,newGroupB,new ArrayList(groupC))){

返回true;
}

返回false; //未找到相等
}

//求和一个列表
私有静态int sumList(List< Integer> list){

/ * a Java 8之前的版本
int sum = 0;
for(int i:list){sum + = i;}
返回总和;
* /
返回list.stream()。mapToInt(Integer :: intValue).sum();
}

您可以更改 mySplit(int。 ..nums) mySplit(int [] nums)并由 mySplit(new int [] {2 ,4,2})


Given task: "Write function named 'mysplit' that gets an int array and calls a recursive method named 'MySplit'.The function MySplit needs to check if the current array can be divided into 2 groups which follow these conditions:

*The Sum of each group must be equal.

*The occurence of each number is only once in each group(this clause is meant to prevent the user from adding/duplicating numbers in order to get equality between the 2 groups=first clause).

*All the numbers that can be divided by 5 must be in the same group.

*All the numbers that can be divided by 3 (and not by 5) must be in the second group.

I have written this code, but reached a dead end.I fail to fulfil the first 2 clauses of the task and cant think of a way to insert it in my code.

This the code I have written:

public static boolean mySplit(int[] nums)
{
    return MySplit(nums,0,0,0);
}

public static boolean MySplit(int[]arr,int result_5,int result_3,int pointer)//2 groups: 'result_5' & 'result_3' and pointer to iterate through an array
{
    if(arr[pointer]%5==0)
    {
        return MySplit(arr,result_5+arr[pointer],result_3,pointer++);
    }
    else if(arr[pointer]%3==0)
    {
        return MySplit(arr,result_5,result_3+arr[pointer],pointer++);
    }

}

*Edit: **Examples:

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

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

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

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

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

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

解决方案

Note the comments. If more clarification is needed do not hesitate to ask:

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();
}

You can change the signature of mySplit(int...nums) to mySplit(int[] nums) and invoke it by mySplit(new int[]{2,4,2})

这篇关于使用递归按特定条件将数组分为2组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆