第七周作业——背包算法

  1. 背包算法的实现如下:包括物品类的编码和主文件的两大部分:
  2. 主文件:
  3. package main;  
  4.   
  5. import java.io.BufferedReader;  
  6. import java.io.File;  
  7. import java.io.FileNotFoundException;  
  8. import java.io.FileReader;  
  9. import java.io.IOException;  
  10. import java.util.ArrayList;  
  11. import java.util.List;  
  12.   
  13. public class Knapsack {  
  14.     public static void main(String[] args) {  
  15.         String path="E:\\Knapsack.txt";  
  16.         try {  
  17.             List list=getGoods(path);  
  18.             list=getlist(list);  
  19.             int W=0,V=0;  
  20.             System.out.println(list.size());  
  21.             for(int i=0;i<list.size();i++){  
  22.                 System.out.println(((goods)list.get(i)).getweight()+" "+((goods)list.get(i)).getvalue());  
  23.                 //System.out.println(((goods)list.get(i)).getid());  
  24.             }  
  25.             W=getWeight(list);  
  26.             V=getValue(list);  
  27.             int[] Id=getId(list);  
  28.             System.out.println(W);  
  29.             System.out.println(V);  
  30.             for(int k=0;k<Id.length;k++){  
  31.                 if(Id[k]!=0)  
  32.                 System.out.println(Id[k]);  
  33.             }  
  34.         } catch (IOException e) {  
  35.             // TODO Auto-generated catch block  
  36.             e.printStackTrace();  
  37.         }  
  38.           
  39.     }  
  40.     //读取文件中的数据  
  41.     public static List getGoods(String path)throws IOException{  
  42.         List list=new ArrayList();  
  43.         FileReader file=new FileReader(path);  
  44.         BufferedReader bu=new BufferedReader(file);  
  45.         String mun=bu.readLine();  
  46.         String[] str=mun.split("  ");  
  47.         int id=0;//物品的id编号  
  48.         while(mun!=null){  
  49.             if(str[0]==mun)  
  50.                 str=mun.split(" ");  
  51.             {  
  52.                 goods gd=new goods();  
  53.                 gd.setid(id);  
  54.                 gd.setweight(Integer.parseInt(str[0]));  
  55.                 gd.setvalue(Integer.parseInt(str[1]));  
  56.                 list.add(gd);  
  57.                 id++;  
  58.             }  
  59.             mun=bu.readLine();  
  60.             if(mun!=null)  
  61.             str=mun.split("  ");  
  62.         }  
  63.         return list;  
  64.     }  
  65.     //将全部物品按重量由小到大排序  
  66.     public static List getlist1(List list){  
  67.         List lt=new ArrayList();  
  68.         goods gd=(goods)list.get(0);  
  69.         goods gs=new goods();  
  70.         goods t=new goods();  
  71.         lt.add(gd);  
  72.         for(int i=1;i<list.size();i++){  
  73.             gd=(goods)list.get(i);  
  74.             for(int j=(i+1);j<list.size();j++){  
  75.                 gs=(goods)list.get(j);  
  76.                 if(gs.getweight()<gd.getweight()){  
  77.                     t.setgoods(gs);  
  78.                     gs.setgoods(gd);  
  79.                     gd.setgoods(t);  
  80.                 }  
  81.             }  
  82.             lt.add(gd);  
  83.         }  
  84.         return lt;  
  85.     }  
  86.     //将全部按重量排序号的物品按价值由大到小排序  
  87.     public static List getlist(List list){  
  88.         //list=getlist1(list);//全部物品按重量由小到大排序好的列表  
  89.         List li=new ArrayList();  
  90.         goods gd=(goods)list.get(0);  
  91.         goods gs=new goods();  
  92.         goods t=new goods();  
  93.         li.add(gd);  
  94.         for(int i=1;i<list.size();i++){  
  95.             gd=(goods)list.get(i);  
  96.             for(int j=(i+1);j<list.size();j++){  
  97.                 gs=(goods)list.get(j);  
  98.                 if(gs.getvalue()>gd.getvalue()){  
  99.                     t.setgoods(gs);  
  100.                     gs.setgoods(gd);  
  101.                     gd.setgoods(t);  
  102.                 }  
  103.             }  
  104.             li.add(gd);  
  105.         }  
  106.         return li;  
  107.     }  
  108.       
  109.     public static int getWeight(List list){  
  110.         goods g=(goods)list.get(0);  
  111.         int w=0,v=0;  
  112.         int w0=g.getweight();  
  113.         w=g.getweight();  
  114.         int k=0;//物品数量  
  115.         for(int i=1;i<list.size();i++){  
  116.             g=(goods)list.get(i);  
  117.             while(w0>0){  
  118.                 if(k==5)  
  119.                     break;  
  120.                 k++;  
  121.                 w0=w0-g.getweight();  
  122.                 v=v+g.getvalue();  
  123.                 if(w0<0){  
  124.                     k--;  
  125.                     w0=w0+g.getweight();  
  126.                     v=v-g.getvalue();  
  127.                     break;  
  128.                 }  
  129.             }  
  130.         }  
  131.         w=w-w0;  
  132.         return w;  
  133.     }  
  134.     public static int getValue(List list){  
  135.         goods g=(goods)list.get(0);  
  136.         int w=0,v=0;  
  137.         int w0=g.getweight();  
  138.         w=g.getweight();  
  139.         int k=0;//物品数量  
  140.         for(int i=1;i<list.size();i++){  
  141.             g=(goods)list.get(i);  
  142.             while(w0>0){  
  143.                 if(k==5)  
  144.                     break;  
  145.                 k++;  
  146.                 w0=w0-g.getweight();  
  147.                 v=v+g.getvalue();  
  148.                 if(w0<0){  
  149.                     k--;  
  150.                     w0=w0+g.getweight();  
  151.                     v=v-g.getvalue();  
  152.                     break;  
  153.                 }  
  154.             }  
  155.         }  
  156.         return v;  
  157.     }  
  158.     public static int[] getId(List list){  
  159.         int[] Id=new int[list.size()];  
  160.         goods g=(goods)list.get(0);  
  161.         int w0=g.getweight();  
  162.         int w=w0;  
  163.         int k=0;//物品数量  
  164.         for(int i=1;i<list.size();i++){  
  165.             g=(goods)list.get(i);  
  166.             while(w0>0){  
  167.                 if(k==5)  
  168.                     break;  
  169.                 k++;  
  170.                 w0=w0-g.getweight();  
  171.                 if(w0<0){  
  172.                     k--;  
  173.                     w0=w0+g.getweight();  
  174.                     break;  
  175.                 }  
  176.             }  
  177.             if(w0!=w)  
  178.                 Id[i]=g.getid();  
  179.             w=w0;  
  180.         }  
  181.         return Id;  
  182.     }  
  183. }  
  184. jean物品类实现:
    1. package main;  
    2.   
    3. public class goods {  
    4.     private int weight;  
    5.     private int value;  
    6.     private int id;  
    7.       
    8.     public goods(){  
    9.         this.id=0;//物品的编号  
    10.         this.weight=0;//物品的重量  
    11.         this.value=0;//物品的价值  
    12.     }  
    13.     public void setgoods(goods g){  
    14.         this.weight=g.weight;  
    15.         this.id=g.id;  
    16.         this.value=g.value;  
    17.     }  
    18.     public int getid(){  
    19.         return this.id;  
    20.     }  
    21.     public int getweight(){  
    22.         return this.weight;  
    23.     }  
    24.     public int getvalue(){  
    25.         return this.value;  
    26.     }  
    27.     public void setid(int id){  
    28.         this.id=id;  
    29.     }  
    30.     public void setweight(int weight){  
    31.         this.weight=weight;  
    32.     }  
    33.     public void setvalue(int value){  
    34.         this.value=value;  
    35.     }  
    36.   
    37. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值