class Placa {
public String name;
public int days;
public int weight;
public Placa(){}
public Placa(String n, int w, int v){
this.name = n;
this.days = v;
this.weight = w;
}
public static int getMaxValueForBackpack(int capacity, Placa places[], int commodityCount) {
int maxValue = -1;
int maxValuePos = -1;
int sum = 0;
for (int i = 0; i < Math.pow(2,commodityCount); i++) {
int pos = i;
int weight = 0;
int value = 0;
int curBit = 0;
while (pos > 0) {
int res = pos & 1;
if (res == 1) {
weight += places[curBit].weight;
value += places[curBit].days;
}
pos = pos >> 1;
++curBit;
}
if (weight <= capacity && maxValue < value) {
maxValue = value;
maxValuePos = i;
}
}
if (maxValuePos > 0) {
int curBit = 0;
while (maxValuePos > 0) {
int res = maxValuePos & 1;
if (res == 1) {
System.out.print(places[curBit].name+"("+places[curBit].weight+","+places[curBit].days+");\n");
sum += places[curBit].days;
}
maxValuePos = maxValuePos >> 1;
++curBit;
}
}
return sum;
}
public static void main(String[] args) {
Placa commodities[] = {
new Placa("故宫", 1, 7),
new Placa("颐和园", 2, 8),
new Placa("长城", 3, 9),
new Placa("天坛", 1, 6)};
System.out.println("最优的旅游路线是:");
int sum = getMaxValueForBackpack(4, commodities, 4);
System.out.println("总评分为:" + sum );
}
}