第7周作业1——背包问题

背包问题。对上文中提到的背包问题提供的表1(数据文件下载Knapsack.txt,第一行为背包总重量15,物品数量5;第2-6行,分别为第1-5件物品的重量与价值),W=15,编程计算最终背包所装物品的编号、总重量与总价值。要求能够把构造的二维表格输出到文件KnapsackResult.txt中。

====

[java]  view plain copy
  1. /* 
  2.  * 动态数组的应用 
  3. */  
  4.   
  5. package aex7;  
  6.   
  7. import java.io.*;  
  8. import java.util.*;  
  9.   
  10. public class Knapsack {  
  11.     static int count=0;//背包内物品数量  
  12.     static int weight[]={0,0,0,0,0};//物品质量  
  13.     static int value[]={0,0,0,0,0};//物品价值  
  14.     static int result[]={-1,-1,-1,-1,-1};//背包内物品的最终数量  
  15.       
  16.     public static void main(String[] args){  
  17.         String path="e:/Knapsack.txt";  
  18.         ArrayList<Integer> list=read(path);//将数据文件导入数组  
  19.           
  20.         for(int i=2,j=0;i<list.size();i=i+2,j++){  
  21.             weight[j]=list.get(i);  
  22.         }  
  23.           
  24.         for(int i=3,j=0;i<list.size();i=i+2,j++){  
  25.             value[j]=list.get(i);  
  26.         }  
  27.           
  28.         Knapsack KS=new Knapsack();  
  29.         System.out.println(KS.KSC(list.get(1)-1,list.get(0)));  
  30.         KS.print();  
  31.           
  32.         KS.write(result);  
  33.     }  
  34.       
  35.     public void print(){  
  36.         for(int i=0;i<result.length;i++){  
  37.             System.out.println(result[i]);  
  38.         }  
  39.     }  
  40.       
  41.     public void write(int[] list){  
  42.         File f=new File("e:/KnapsackResult.txt");  
  43.         FileOutputStream fout=null;  
  44.           
  45.         try{  
  46.             fout=new FileOutputStream(f,true);  
  47.               
  48.             for(int i=0;i<list.length;i++){  
  49.                 String s=String.valueOf(list[i]);  
  50.                 String ss=""+s+"\t\n";  
  51.                 fout.write(ss.getBytes());//字符串通过字节流输入数组  
  52.             }  
  53.         }catch(Exception e){  
  54.             e.printStackTrace();  
  55.         }finally{  
  56.             try{  
  57.                 fout.close();  
  58.             }catch(Exception e){  
  59.                 e.printStackTrace();  
  60.             }  
  61.         }  
  62.     }  
  63.       
  64.     //比较价值  
  65.     public int KSC(int n,int count){  
  66.         if(n==-1||count==0)  
  67.             return 0;  
  68.           
  69.         int tmp1=KSC(n-1,count);  
  70.         if(weight[n]>count){  
  71.             result[n]=0;  
  72.             return tmp1;  
  73.         }  
  74.           
  75.         int tmp2=value[n]+KSC(n-1,count-weight[n]);  
  76.         if(tmp1>tmp2){  
  77.             result[n]=0;  
  78.             return tmp1;  
  79.         }  
  80.           
  81.         result[n]=1;  
  82.         return tmp2;  
  83.     }  
  84.       
  85.     //文件写入动态数组  
  86.     public static ArrayList read(String path){  
  87.         ArrayList<Integer> list=new ArrayList<Integer>();  
  88.         BufferedReader input=null;  
  89.           
  90.         try{  
  91.             FileReader in=new FileReader(path);  
  92.             input=new BufferedReader(in);  
  93.             String ss;  
  94.               
  95.             try{  
  96.                 while((ss=input.readLine())!=null){  
  97.                     String[] s=(ss.split("  "));  
  98.                     for(int i=0;i<s.length;i++){  
  99.                         list.add(Integer.parseInt(s[i].trim()));  
  100.                     }  
  101.                 }  
  102.             }catch(IOException e){  
  103.                 e.printStackTrace();  
  104.             }  
  105.               
  106.             in.close();  
  107.             input.close();  
  108.               
  109.         }catch(Exception e){  
  110.             e.printStackTrace();  
  111.         }  
  112.           
  113.         return list;  
  114.     }  
  115. }  


====

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值