greedy knapsack

package com.skex.greedyknapsack;   
public class Goods {   
  /** 物品类型  */   
  private String type;  
      
  /** 背包重量  */   
  private int weight;   
       
  /** 背包物品价值  */   
  private int value;   
  
  /**均价**/
  private float avgValue;
  /*** 
   * 构造器 
   */   
  public Goods(String type,int weight, int value) {  
      this.type=type;
      this.value = value;   
      this.weight = weight;   
      avgValue=value/(float)weight;
  }
  
  
  public String getType() {   
      return type;   
  } 
  
  public int getWeight() {   
      return weight;   
  }   
       
  public int getValue() {   
      return value;   
  } 
  
  public float getAvgValue() {   
      return avgValue;  
  }
       
  public String toString() {   
      return "[type: "+ type +",weight: " + weight + " " + ",value: " + value +",avgValue: "+ avgValue + "]";     
  }   
}

 

package com.skex.greedyknapsack;

import java.util.HashMap;  
import java.util.Iterator;
import java.util.Map;


public class MyTest {
/*
    [背包问题]有一个背包,背包容量是M=150。有7个物品,物品可以分割成任意大小。
            要求尽可能让装入背包中的物品总价值最大,但不能超过总容量。
            物品 A B C D E F G
            重量 35 30 60 50 40 10 25
            价值 10 40 30 50 35 40 30
            */
    
    static int MaxWeight=150;
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Goods A=new Goods("A",35,10);
        Goods B=new Goods("B",30,40);
        Goods C=new Goods("C",60,30);
        Goods D=new Goods("D",50,50);
        Goods E=new Goods("E",40,35);
        Goods F=new Goods("F",10,40);
        Goods G=new Goods("G",25,30);
        
        HashMap<String, Integer> goodsCount=new HashMap<String,Integer>(); 
        goodsCount.put("A", 0);
        goodsCount.put("B", 0);
        goodsCount.put("C", 0);
        goodsCount.put("D", 0);
        goodsCount.put("E", 0);
        goodsCount.put("F", 0);
        goodsCount.put("G", 0); 
        
        Goods[] arryGoods=new  Goods[]{A,B,C,D,E,F,G};
        sortDsc(arryGoods);
        for(int i=0;i<arryGoods.length;i++){
            System.out.println(arryGoods[i].toString());
        }
        
        int index=0;
        int sumWeight=0;
        
        while(index<arryGoods.length){
            
            if(sumWeight+arryGoods[index].getWeight()<=MaxWeight){        
                sumWeight+=arryGoods[index].getWeight();
                Integer oldCount= goodsCount.get(arryGoods[index].getType());
                goodsCount.put(arryGoods[index].getType(), oldCount +1); 
            }else{
                if(sumWeight==MaxWeight){
                    break;
                }else{
                    index+=1;
                }                
            }            
        }
        
         // goodsCount是HashMap对象 
         // goodsCount中的key是String类型,value是Integer类型 
         Integer integ = null; 
         Iterator iter = goodsCount.entrySet().iterator(); 
         while(iter.hasNext()) { 
             Map.Entry entry = (Map.Entry)iter.next(); 
             // 获取key 
             String key = (String)entry.getKey(); 
             // 获取value 
             integ = (Integer)entry.getValue(); 
             System.out.println(key +" => "+integ);
         }
        
         System.out.println("----------------------------");
         
        for(char c='A';c<='G';c++){
            String key = String.valueOf(c);
            Integer Count= goodsCount.get(key);
            System.out.println(key +" => "+Count);            
        }
        
        /*
        [type: F,weight: 10 ,value: 40,avgValue: 4.0]
        [type: B,weight: 30 ,value: 40,avgValue: 1.3333334]
        [type: G,weight: 25 ,value: 30,avgValue: 1.2]
        [type: D,weight: 50 ,value: 50,avgValue: 1.0]
        [type: E,weight: 40 ,value: 35,avgValue: 0.875]
        [type: C,weight: 60 ,value: 30,avgValue: 0.5]
        [type: A,weight: 35 ,value: 10,avgValue: 0.2857143]
        D => 0
        E => 0
        F => 15
        G => 0
        A => 0
        B => 0
        C => 0
        ----------------------------
        A => 0
        B => 0
        C => 0
        D => 0
        E => 0
        F => 15
        G => 0
        */         
        
    }
    
    public static void sortDsc(Goods[] arryGoods){
        for(int i=0;i<arryGoods.length-1;i++){
            int maxIndex=i;
            for(int j=i+1;j<arryGoods.length;j++){
                if(arryGoods[maxIndex].getAvgValue()<arryGoods[j].getAvgValue()){
                    maxIndex=j;
                }
            }
            if(maxIndex!=i){
                Goods tmp=arryGoods[i];
                arryGoods[i]=arryGoods[maxIndex];
                arryGoods[maxIndex]=tmp;
            }
        }
    }

}

 

posted @ 2018-02-02 10:48  sky20080101  阅读(87)  评论(0)    收藏  举报