递归方法没有正确执行

时间:2014-10-03 13:10:14

标签: java recursion subset-sum

我有一个介绍级Java类的编程任务(子集求和问题) - 由于某种原因,我的递归方法没有正确执行(它只是直接到方法的结尾并打印出来排序清单)。任何帮助将不胜感激 - 我是一个新手和递归函数真的让我感到困惑。

package programmingassignment3;

import java.io.*;
import java.util.*;

public class ProgrammingAssignment3 {

    static int TARGET = 10;
    static ArrayList<Integer> list = new ArrayList<>();
    static int SIZE = list.size();

    public static void main(String[] args) {
        populateSortSet();
        sumInt(list);
        recursiveSS(list);
    }//main

    public static void populateSortSet() {
        try {
            File f = new File("set0.txt");
            Scanner input = new Scanner(f);
            while (input.hasNext()) {
                int ele = input.nextInt();
                if (ele < TARGET && !list.contains(ele)) {
                    list.add(ele);
                }//if
            }//while
            Collections.sort(list);
        }//try
        catch (IOException e) {
            e.printStackTrace();
        }//catch
    }//populateSet

    public static void recursiveSS(ArrayList<Integer> Alist) {
        if (Alist.size() == SIZE) {
            if (sumInt(Alist) == TARGET) {
                System.out.println("The integers that equal " + TARGET + "are: " + Alist);
            } //if==TARGET  
        }//if==SIZE
        else {
            for (int i = 0; i < SIZE; i++) {
                ArrayList<Integer> list1 = new ArrayList<>(Alist);
                ArrayList<Integer> list0 = new ArrayList<>(Alist);
                list1.add(1);
                list0.add(0);
                if (sumInt(list0) < TARGET) {
                    recursiveSS(list0);
                }//if
                if (sumInt(list1) < TARGET) {
                    recursiveSS(list1);
                }//if
            }//for
        }//else
        System.out.println("echo" + Alist);
    }//recursiveSS

    public static int sumInt(ArrayList<Integer> Alist) {
        int sum = 0;
        for (int i = 0; i < SIZE - 1; i++) {
            sum += Alist.get(i);
        }//for
        if (Alist.size() == TARGET) {
            sum += Alist.get(Alist.size() - 1);
        }//if
        return sum;
    }//sumInt
}//class

3 个答案:

答案 0 :(得分:1)

你在课堂上做的这件事:

static ArrayList<Integer> list = new ArrayList<>();
static int SIZE = list.size();

表示SIZE将启动到0,并保留0(即使您向列表中添加元素。)

这意味着for-loop内的代码将被执行0次。

尝试类似:

public class ProgrammingAssignment3 {
    private static int initialSize;

    //...
    public static void populateSortSet() {
        //populate the list
        initialSize = list.size();
    }

因此,在实际填充列表之前,不要设置size变量的值。

话虽如此,您的代码中还有其他一些奇怪的东西,所以我认为您需要准确指定您在此处尝试解决的问题。

答案 1 :(得分:0)

以下是我的表现。我希望它澄清停止条件和递归。如您所见,静态方法不是问题:

import java.util.ArrayList;
import java.util.List;

/**
 * Demo of recursion
 * User: mduffy
 * Date: 10/3/2014
 * Time: 10:56 AM
 * @link http://stackoverflow.com/questions/26179574/recursive-method-not-properly-executing?noredirect=1#comment41047653_26179574
 */
public class RecursionDemo {

    public static void main(String[] args) {
        List<Integer> values = new ArrayList<Integer>();
        for (String arg : args) {
            values.add(Integer.valueOf(arg));
        }
        System.out.println(String.format("input values : %s", values));
        System.out.println(String.format("iterative sum: %d", getSumUsingIteration(values)));
        System.out.println(String.format("recursive sum: %d", getSumUsingRecursion(values)));
    }

    public static int getSumUsingIteration(List<Integer> values) {
        int sum = 0;
        if (values != null) {
            for (int value : values) {
                sum += value;
            }
        }
        return sum;
    }

    public static int getSumUsingRecursion(List<Integer> values) {
        if ((values == null) || (values.size() == 0)) {
            return 0;
        } else {
            if (values.size() == 1) { // This is the stopping condition
                return values.get(0);
            } else {
                return values.get(0) + getSumUsingRecursion(values.subList(1, values.size())); // Here is recursion
            }
        }
    }
}

以下是我用来测试它的情况:

input values : [1, 2, 3, 4, 5, 6]
iterative sum: 21
recursive sum: 21

Process finished with exit code 0

答案 2 :(得分:0)

谢谢大家。我非常感谢你的帮助。我确实找出了问题,解决方案如下(为了阅读@duffymo的乐趣,删除了大括号评论):

public class ProgrammingAssignment3 {

    static int TARGET = 6233;
    static ArrayList<Integer> set = new ArrayList<>();
    static int SIZE;
    static int count = 0;

    public static void populateSortSet() {
        try {
            File f = new File("set3.txt");
            Scanner input = new Scanner(f);
            while (input.hasNext()) {
                int ele = input.nextInt();
                if (ele < TARGET && !set.contains(ele)) {
                    set.add(ele);
                }
            }
            Collections.sort(set);
            SIZE = set.size();
            System.out.println("The original sorted set: " + set + "\t subset sum = " + TARGET);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void recursiveSS(ArrayList<Integer> list) {
        if (list.size() == SIZE) {
            if (sumInt(list) == TARGET) {
                System.out.print("The Bit subset is: " + list + "\t");
                System.out.println("The subset is: " + getSubset(list));
                count++;
            }  
        }
        else {
            ArrayList<Integer> list1 = new ArrayList<>(list);//instantiate list1
            ArrayList<Integer> list0 = new ArrayList<>(list);//instantiate list0
            list1.add(1);
            list0.add(0);
            if (sumInt(list0) <= TARGET) {
                recursiveSS(list0);
            }
            if (sumInt(list1) <= TARGET) {
                recursiveSS(list1);
            }
        }
    }

    public static int sumInt(ArrayList<Integer> list) {
        int sum = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == 1) {
                sum += set.get(i);
            }
        }
        return sum;
    }

    public static ArrayList<Integer> getSubset(ArrayList<Integer> list) {
        ArrayList<Integer> l = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == 1) {
                l.add(set.get(i));
            }
        }
        return l;
    }
}