-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageProblem.java
More file actions
219 lines (204 loc) · 6.21 KB
/
PackageProblem.java
File metadata and controls
219 lines (204 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//You want to send your friend a package with different things.
//Each thing you put inside the package has such parameters as index number, weight and cost.
//The package has a weight limit.
//Your goal is to determine which things to put into the package so that the total weight is less than
//or equal to the package limit and the total cost is as large as possible.
//You would prefer to send a package which weights less in case there is more than one package with the same price.
//INPUT SAMPLE:
//Your program should accept as its first argument a path to a filename.
//The input file contains several lines. Each line is one test case.
//Each line contains the weight that the package can take (before the colon) and the list of things you need to choose.
//Each thing is enclosed in parentheses where the 1st number is a thing's index number, the 2nd is its weight and the 3rd is its cost.
//E.g.
//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)
//OUTPUT SAMPLE:
//For each set of things that you put into the package provide a list (items’ index numbers are separated by comma). E.g.
//4
//-
//2,7
//8,9
//CONSTRAINTS:
//Max weight that a package can take is ≤ 100
//There might be up to 15 items you need to choose from
//Max weight and cost of an item is ≤ 100
package com.practice;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PackageProblem{
public static void parserpackages(String[] sub2,List<Package> packages,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);
packages.add(condidat);
}
}
}
static String getOptimumFor(List<Package> packages, int r,int NmaxWeight){
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[packages.size()];
for(int i=0;i<packages.size();i++)
{
arr[i]=i;
}
getCombination(arr, data, res, 0, 0);
//res contien les combinaison de longeur r
//calcule des somme cost et weight
for(int i=0;i<=res.size()-r;i+=r)
{
double somWeight=0;
double somCost=0;
for(int j=0;j<r;j++)
{
somWeight+=packages.get(res.get(i+j)).getWeight();
somCost+=packages.get(res.get(i+j)).getCost();
}
if(somWeight<=NmaxWeight){
if((somCost>maxCost)||((somCost==maxCost)&&(somWeight<=maxWeight)))
{
indexSolution=i;
maxWeight=somWeight;
maxCost=somCost;
}
}
}
for(int k=indexSolution;k<r+indexSolution;k++)
{
returnData+=res.get(k)+",";
}
return returnData+maxCost+","+maxWeight;
}
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("D:/packageproblem.txt");
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String line;
List<Package> packages=new ArrayList<Package>();
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)
{
packages.clear();
parserpackages(sub2,packages,N);
double maxCost=0;
double maxWeight=0;
for(int i=1;i<=packages.size();i++)
{
String resultat=getOptimumFor(packages,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+=packages.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() {
super();
}
public Package(int id, double weight, double cost) {
super();
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;
}
@Override
public String toString() {
return "Package [id=" + id + ", weight=" + weight + ", cost=" + cost
+ "]";
}
}