返回Knapsack中的索引而不是java中的值

时间:2017-09-03 11:11:37

标签: java arrays list return-value dynamic-programming

我想在背包问题中返回包的索引。

我的输入是:

  • 索引数组
  • 重量数组
  • 价格数组
  • 行李容量

输出应为: 该包的指数(即每个包具有指数重量和价格)

    package main.java.assessment;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Problem1 {

    private static final String FILENAME = "data"; //path of the file

    public static void main(String[] args) {
        BufferedReader br = null;
        FileReader fr = null;

        try {

            //reding file
            fr = new FileReader(FILENAME);
            br = new BufferedReader(fr);

            String sCurrentLine;
            Double capacity,result; //maximum weight that package can take
            String[] list;
            List<String> pack;  //each package
            List<Integer> index; //index of package which we want to return as output
            List<Double> price,weight; //price and weight of each package

            //reading file line by line
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
                index= new ArrayList<Integer>();
                price= new ArrayList<Double>();
                weight= new ArrayList<Double>();

                //get capcity ,index,weight,price of each package using string manipulation 
                list=sCurrentLine.split("\\s");
                capacity=Double.parseDouble(list[0]);
                for(int i=2;i<list.length-1;i++) {
                    pack=Arrays.asList(list[i].replaceAll("[()]", "").split(","));
                    index.add(Integer.parseInt(pack.get(0)));
                    weight.add(Double.parseDouble(pack.get(1)));
                    price.add(Double.parseDouble(pack.get(2).replaceAll("\\$", "")));
                }
                result=knack(capacity,index.size(),weight,price);
                if(result == 0)
                System.out.println("_");
                else
                    System.out.println(result);



            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                if (br != null)
                    br.close();

                if (fr != null)
                    fr.close();

            } catch (IOException ex) {

                ex.printStackTrace();

            }

        }

    }

    private static Double max(Double a, Double b) {
        return a > b ? a : b;
    }

    private static Double knack(Double W, int N, List<Double> wt, List<Double> val) {
        if (W == 0 || N == 0)
            return 0d;
        if (wt.get(N-1) > W) {
            return knack(W, N - 1, wt, val);
        } else {
            return max(val.get(N-1) + knack(W - wt.get(N-1), N - 1, wt, val), knack(W, N - 1, wt, val));
        }

    }

}

截至目前,我的输出是最大值。我无法想出用什么方法来获得指数而不是最高价格。

我无法附加输入文件,但输入是

81 : (1,53.38,$45) (2,88.62,$98) (3,78.48,$3) (4,72.30,$76) (5,30.18,$9) (6,46.34,$48)
8 : (1,15.3,$34)
75 : (1,85.31,$29) (2,14.55,$74) (3,3.98,$16) (4,26.24,$55) (5,63.69,$52) (6,76.25,$75) (7,60.02,$74) (8,93.18,$35) (9,89.95,$78)
56 : (1,90.72,$13) (2,33.80,$40) (3,43.15,$10) (4,37.97,$16) (5,46.81,$36) (6,48.77,$79) (7,81.80,$45) (8,19.36,$79) (9,6.76,$64)

0 个答案:

没有答案