package com.fhj.test;
public class fhj {
public static void main(String[] args) {
Goal [] goals = {
new Goal ("故宫", 1, 7),
new Goal ("颐和园", 2, 8),
new Goal ("长城", 3, 9),
new Goal ("天坛", 1, 6)
};
System.out.println("旅游行程最优化路线:");
int score = maxBackpackByValue(4, goals, 4);
System.out.println("总评分:" + score);
}
public static int maxBackpackByValue(int num, Goal [] goals, int count) {
int maxValue = -1;
int maxValuePos = -1;
int sum = 0;
for (int i = 0; i < Math.pow(2, count); i++) {
int pos = i;
int weight = 0;
int value = 0;
int index = 0;
while (pos > 0) {
int res = pos & 1;
if (res == 1) {
weight += goals[index].weight;
value += goals[index].day;
}
pos = pos >> 1;
++index;
}
if (weight <= num && maxValue < value) {
maxValue = value;
maxValuePos = i;
}
}
if (maxValuePos > 0) {
int index = 0;
while (maxValuePos > 0) {
int res = maxValuePos & 1;
if (res == 1) {
System.out.println(goals[index].name);
sum += goals[index].day;
}
maxValuePos = maxValuePos >> 1;
index++;
}
}
return sum;
}
}
class Goal {
public String name;
public int day;
public int weight;
public Goal () {
}
public Goal (String n, int w, int v) {
this.name = n;
this.day = v;
this.weight = w;
}
}