国外二手手表网站,广告设计软件哪个好,有什么专业做心理的网站,wordpress管理员破解有5个物体#xff0c;每个物品只有一个,其重量分别是为2,2,6,5,4,价值分别为6,3,5,4,6,背包的载重量为10,求装入背包的物体及总质量。
计算结果#xff1a;15
package com.lanQiaoFor6;import java.util.ArrayList;
import java.util.TreeSet;public class JAVA_6 {static …有5个物体每个物品只有一个,其重量分别是为2,2,6,5,4,价值分别为6,3,5,4,6,背包的载重量为10,求装入背包的物体及总质量。
计算结果15
package com.lanQiaoFor6;import java.util.ArrayList;
import java.util.TreeSet;public class JAVA_6 {static TreeSetPerson treeSet;static ArrayListPerson arrayList;static Person[] persons;static Person[] realPersons;static int asnprice 0;static int realAsnprice 0;static int weightSum 10;static int count 0;static int size 0;public static void main(String[] args) {persons new Person[5];treeSet new TreeSet();treeSet.add(new Person(2, 6));treeSet.add(new Person(2, 3));treeSet.add(new Person(6, 5));treeSet.add(new Person(5, 4));treeSet.add(new Person(4, 6));arrayList new ArrayList();arrayList.addAll(treeSet);for (int i 0; i 5; i) {Person person arrayList.get(i);if (person.weight weightSum) {continue;}person.flag true;count person.weight;asnprice person.value;persons[size] person;if (i 4) {if (person.value realAsnprice) {realAsnprice (int) person.value;}continue;}dfs(arrayList.get(i 1), count);person.flag false;count - person.weight;asnprice - person.value;persons[size--] null;}System.out.println(realAsnprice);}public static void dfs(Person person, int count) {// TODO Auto-generated method stub// 出口if (weightSum count person.weight) {if (asnprice realAsnprice) {realAsnprice asnprice;realPersons persons.clone();for(int i0;isize;i) {System.out.print (persons[i].value );}System.out.println();}}// 遍历for (int i 0; i 4; i) {if (weightSum count person.weight person.flag ! true) {person.flag true;count person.weight;asnprice person.value;persons[size] person;int index arrayList.indexOf(person);if (index ! arrayList.size()) {dfs(arrayList.get(index 1), count);person.flag false;count - person.weight;asnprice - person.value;persons[size--] null;}}}}
}class Person implements ComparablePerson {public double value;public double weight;public boolean flag false;public Person(double weight, double value) {// TODO Auto-generated constructor stubthis.value value;this.weight weight;}public int compareTo(Person o) {// TODO Auto-generated method stubif ((this.value / this.weight) (o.value / o.weight)) {return -1;} else {return 1;}}}