代码测试 - 最大化给定包装重量的成本

时间:2018-04-13 21:03:49

标签: java

您希望向朋友发送包含不同内容的套餐。你放在包装里的每件东西都有参数,如索引号,重量和成本。包裹有重量限制。您的目标是确定将哪些东西放入包装中,以使总重量小于或等于包装限制,并且总成本尽可能大。

如果有多个包装且价格相同,您更愿意寄送重量较轻的包裹。

INPUT SAMPLE:

您的程序应该接受文件名路径作为其第一个参数。输入文件包含多行。每一行都是一个测试用例。

每行包含包可以承担的重量(在冒号之前)以及您需要选择的事物列表。每个东西都括在括号中,第一个数字是东西的索引号,第二个是它的重量,第三个是它的成本。例如:

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)

输出样本:

对于放入包中的每组内容,都会提供一个列表(项目的索​​引号用逗号分隔)。 例如。

4 //for package1
- //for package2
2,7 //for package3
8,9 //for package4

约束:

包裹可以承受的最大重量≤100最多可能需要选择15件物品最大重量和物品成本≤100

是否有比https://discuss.codechef.com/questions/104934/package-problemin-java

更优雅易懂的解决方案

1 个答案:

答案 0 :(得分:0)

这是代码(KnapSack):

public class FriendPackageKnapsack {

    public static void parserThings(String[] sub2, List<Package> things, 
            double maxWeight) {
        for (int i = 0; i < sub2.length; i++) {
            String[] sub3 = (sub2[i].substring(1, sub2[i].length() - 1)).split(",");
            int id = Integer.parseInt(sub3[0]);
            double weight = Double.parseDouble(sub3[1]);
            double cost = Double.parseDouble(sub3[2].substring(1, sub3[2].length()));
            if (weight <= maxWeight) {
                Package condidat = new Package(id, weight, cost);
                things.add(condidat);
            }
        }
    }

    public static String getOptimumFor(List<Package> things, int r, int maxWt) {
        int indexSolution = 0;
        String returnData = "";
        double maxWeight = 0;
        double maxCost = 0;
        int[] data = new int[r];
        List<Integer> res = new ArrayList<Integer>();
        int[] arr = new int[things.size()];
        for (int i = 0; i < things.size(); i++) {
            arr[i] = i;
        }
        getCombination(arr, data, res, 0, 0);

        for (int i = 0; i <= res.size() - r; i += r) {
            double someWeight = 0;
            double someCost = 0;
            for (int j = 0; j < r; j++) {
                someWeight += things.get(res.get(i + j)).getWeight();
                someCost += things.get(res.get(i + j)).getCost();
            }
            if (someWeight <= maxWt) {
                if ((someCost > maxCost)
                        || ((someCost == maxCost) && (someWeight <= maxWeight))) {
                    indexSolution = i;
                    maxWeight = someWeight;
                    maxCost = someCost;
                }
            }
        }
        for (int k = indexSolution; k < r + indexSolution; k++) {
            returnData += res.get(k) + ",";
        }
        return returnData + maxCost + "," + maxWeight;
    }

    public static void getCombination(int[] arr, int[] data, List<Integer> res, int start,
            int index) {
        if (index == data.length) {
            for (int j = 0; j < data.length; j++) { res.add(data[j]); }
            return;
        }
        for (int i = start; i < arr.length && arr.length - i >= data.length - index; i++) {
            data[index] = arr[i];
            getCombination(arr, data, res, i + 1, index + 1);
        }
    }

    public static void main(String[] args) {
        File file = new File("packageproblem.txt");
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
        } 
        catch (FileNotFoundException e) { e.printStackTrace(); }
        String line = "";
        List<Package> things = new ArrayList<>();
        try {
            while ((line = in.readLine()) != null) {
                String s = "";
                // Parsing line
                String[] sub1 = line.split(" : ");
                int N = Integer.parseInt(sub1[0]);
                String[] sub2 = sub1[1].split(" ");
                if (sub2.length > 1) {
                    things.clear();
                    parserThings(sub2, things, N);

                    double maxCost = 0;
                    double maxWeight = 0;
                    for (int i = 1; i <= things.size(); i++) {
                        String resultat = getOptimumFor(things, i, N);
                        // System.out.println(resultat);
                        String[] sub4 = resultat.split(",");
                        double cost = Double.parseDouble(sub4[sub4.length - 2]);
                        double weight = Double.parseDouble(sub4[sub4.length - 1]);
                        if (cost == maxCost) {
                            if (weight < maxWeight) {
                                maxCost = cost;
                                maxWeight = weight;
                                s = resultat;
                            }
                        }
                        if (cost > maxCost) {
                            maxCost = cost;
                            maxWeight = weight;
                            s = resultat;
                        }  
                    }
                    // System.out.println(s);
                    String[] sub5 = s.split(",");
                    String ss = "";
                    for (int i = 0; i < sub5.length - 2; i++) {
                        ss += things.get(Integer.parseInt(sub5[i])).getId() + ",";
                    }
                    if (ss.equals("")) { System.out.println("-"); }
                    else { System.out.println(ss.substring(0, ss.length() - 1)); }

                } 
                else {  System.out.println("-"); }
            }

        } 
        catch (IOException e) { e.printStackTrace(); }
    }
}

class Package {
    private int id;
    private double weight;
    private double cost;

    public Package(int id, double weight, double cost) {
        this.id = id;
        this.weight = weight;
        this.cost = cost;
    }

    public int getId() { return id; }

    public void setId(int id) { this.id = id; }

    public double getWeight() { return weight; }

    public void setWeight(double weight) { this.weight = weight; }

    public double getCost() { return cost; }

    public void setCost(double cost) { this.cost = cost; }
}

代码取自here