- 背包算法的实现如下:包括物品类的编码和主文件的两大部分:
- 主文件:
- package main;
- 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 Knapsack {
- public static void main(String[] args) {
- String path="E:\\Knapsack.txt";
- try {
- List list=getGoods(path);
- list=getlist(list);
- int W=0,V=0;
- System.out.println(list.size());
- for(int i=0;i<list.size();i++){
- System.out.println(((goods)list.get(i)).getweight()+" "+((goods)list.get(i)).getvalue());
- //System.out.println(((goods)list.get(i)).getid());
- }
- W=getWeight(list);
- V=getValue(list);
- int[] Id=getId(list);
- System.out.println(W);
- System.out.println(V);
- for(int k=0;k<Id.length;k++){
- if(Id[k]!=0)
- System.out.println(Id[k]);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //读取文件中的数据
- public static List getGoods(String path)throws IOException{
- List list=new ArrayList();
- FileReader file=new FileReader(path);
- BufferedReader bu=new BufferedReader(file);
- String mun=bu.readLine();
- String[] str=mun.split(" ");
- int id=0;//物品的id编号
- while(mun!=null){
- if(str[0]==mun)
- str=mun.split(" ");
- {
- goods gd=new goods();
- gd.setid(id);
- gd.setweight(Integer.parseInt(str[0]));
- gd.setvalue(Integer.parseInt(str[1]));
- list.add(gd);
- id++;
- }
- mun=bu.readLine();
- if(mun!=null)
- str=mun.split(" ");
- }
- return list;
- }
- //将全部物品按重量由小到大排序
- public static List getlist1(List list){
- List lt=new ArrayList();
- goods gd=(goods)list.get(0);
- goods gs=new goods();
- goods t=new goods();
- lt.add(gd);
- for(int i=1;i<list.size();i++){
- gd=(goods)list.get(i);
- for(int j=(i+1);j<list.size();j++){
- gs=(goods)list.get(j);
- if(gs.getweight()<gd.getweight()){
- t.setgoods(gs);
- gs.setgoods(gd);
- gd.setgoods(t);
- }
- }
- lt.add(gd);
- }
- return lt;
- }
- //将全部按重量排序号的物品按价值由大到小排序
- public static List getlist(List list){
- //list=getlist1(list);//全部物品按重量由小到大排序好的列表
- List li=new ArrayList();
- goods gd=(goods)list.get(0);
- goods gs=new goods();
- goods t=new goods();
- li.add(gd);
- for(int i=1;i<list.size();i++){
- gd=(goods)list.get(i);
- for(int j=(i+1);j<list.size();j++){
- gs=(goods)list.get(j);
- if(gs.getvalue()>gd.getvalue()){
- t.setgoods(gs);
- gs.setgoods(gd);
- gd.setgoods(t);
- }
- }
- li.add(gd);
- }
- return li;
- }
- public static int getWeight(List list){
- goods g=(goods)list.get(0);
- int w=0,v=0;
- int w0=g.getweight();
- w=g.getweight();
- int k=0;//物品数量
- for(int i=1;i<list.size();i++){
- g=(goods)list.get(i);
- while(w0>0){
- if(k==5)
- break;
- k++;
- w0=w0-g.getweight();
- v=v+g.getvalue();
- if(w0<0){
- k--;
- w0=w0+g.getweight();
- v=v-g.getvalue();
- break;
- }
- }
- }
- w=w-w0;
- return w;
- }
- public static int getValue(List list){
- goods g=(goods)list.get(0);
- int w=0,v=0;
- int w0=g.getweight();
- w=g.getweight();
- int k=0;//物品数量
- for(int i=1;i<list.size();i++){
- g=(goods)list.get(i);
- while(w0>0){
- if(k==5)
- break;
- k++;
- w0=w0-g.getweight();
- v=v+g.getvalue();
- if(w0<0){
- k--;
- w0=w0+g.getweight();
- v=v-g.getvalue();
- break;
- }
- }
- }
- return v;
- }
- public static int[] getId(List list){
- int[] Id=new int[list.size()];
- goods g=(goods)list.get(0);
- int w0=g.getweight();
- int w=w0;
- int k=0;//物品数量
- for(int i=1;i<list.size();i++){
- g=(goods)list.get(i);
- while(w0>0){
- if(k==5)
- break;
- k++;
- w0=w0-g.getweight();
- if(w0<0){
- k--;
- w0=w0+g.getweight();
- break;
- }
- }
- if(w0!=w)
- Id[i]=g.getid();
- w=w0;
- }
- return Id;
- }
- }
- jean物品类实现:
-
- package main;
- public class goods {
- private int weight;
- private int value;
- private int id;
- public goods(){
- this.id=0;//物品的编号
- this.weight=0;//物品的重量
- this.value=0;//物品的价值
- }
- public void setgoods(goods g){
- this.weight=g.weight;
- this.id=g.id;
- this.value=g.value;
- }
- public int getid(){
- return this.id;
- }
- public int getweight(){
- return this.weight;
- }
- public int getvalue(){
- return this.value;
- }
- public void setid(int id){
- this.id=id;
- }
- public void setweight(int weight){
- this.weight=weight;
- }
- public void setvalue(int value){
- this.value=value;
- }
- }
第七周作业——背包算法
最新推荐文章于 2024-08-27 12:22:52 发布