下面将一个集合传递给此方法,并且还会传入一个条形长度.如果从条形长度中移除了集合中的某些数字,则解决方案应输出集合中的数字,这些数字会产生最少量的浪费.因此,条形长度10,设置包括6,1,4,因此解决方案是6和4,并且浪费是0.我在通过集合回溯的条件有一些麻烦.我也尝试使用浪费的"全局"变量来帮助回溯方面,但无济于事.
SetInt是一个手动创建的集合实现,它可以添加,删除,检查集合是否为空并从集合中返回最小值.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package recback;
public class RecBack {
public static int WASTAGE = 10;
public static void main(String[] args) {
int[] nums = {6,1,4};
//Order Numbers
int barLength = 10;
//Bar length
SetInt possibleOrders = new SetInt(nums.length);
SetInt solution = new SetInt(nums.length);
//Set Declarration
for (int i = 0; i < nums.length; i++)possibleOrders.add(nums[i]);
//Populate Set
SetInt result = tryCutting(possibleOrders, solution, barLength); …Run Code Online (Sandbox Code Playgroud)