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

来源:互联网 发布:如何解绑农村淘宝 编辑:程序博客网 时间:2024/04/29 18:15

控制台实现代码:

  1. package seven.suanfa.whp;  
  2.   
  3. public class Knapsack {  
  4.   
  5.     /** 
  6.      * 
  7.      */  
  8.     int count=15;  //容量为15  
  9.     int weight[]={4,12,1,2,1}; //物品的重量  
  10.     int value[]={10,4,2,2,1};   //物品的价值  
  11.     int result[]={-1,-1,-1,-1,-1};//初始化背包  
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub  
  14.         Knapsack ks=new Knapsack();  
  15.         System.out.println(ks.Knapsacountk(4,15));  
  16.         ks.print();  
  17.       
  18.           
  19.     }  
  20.     public void print(){  
  21.         for(int i=0;i<result.length;i++)  
  22.         {  
  23.             System.out.println(result[i]);  
  24.         }  
  25.     }  
  26.     public int Knapsacountk(int n ,int count)  
  27.     {  
  28.         if(n==-1||count==0)  
  29.             return 0;  
  30.         int tmp1=Knapsacountk(n-1,count);  
  31.         if(weight[n]>count)  
  32.         {  
  33.             result[n]=0;  
  34.             return tmp1;  
  35.         }  
  36.         int tmp2=value[n]+Knapsacountk(n-1,count-weight[n]);  
  37.         if(tmp1>tmp2)  
  38.         {  
  39.             result[n]=0;  
  40.             return tmp1;  
  41.         }  
  42.         result[n]=1;  
  43.         return tmp2;      
  44.     }  
  45.   
  46. }  
结果:最大价值为15,第1/3/4/5件物品装入背包中。

添加从txt中读取和写入txt代码。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package seven.suanfa.whp;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileOutputStream;  
  6. import java.io.FileReader;  
  7. import java.io.IOException;  
  8. import java.util.ArrayList;  
  9.   
  10. public class Knapsack {  
  11.   
  12.     /** 
  13.      * 
  14.      */  
  15.     static int count=0;  //容量为15  
  16.     static int weight[]={0,0,0,0,0}; //物品的重量  
  17.     static int value[]={0,0,0,0,0}; //物品的价值  
  18.     static int result[]={-1,-1,-1,-1,-1};//初始化背包  
  19.     public static void main(String[] args) {  
  20.         String path = "txt/Knapsack.txt";  
  21.         ArrayList<Integer> list = read(path);  
  22.           
  23.         for(int i=2,j=0;i<list.size();i=i+2,j++){  
  24.               
  25.             weight[j]=list.get(i);  
  26.         }  
  27.         for(int i=3,j=0;i<list.size();i=i+2,j++){  
  28.               
  29.             value[j]=list.get(i);  
  30.         }  
  31.         //控制台打印  
  32.         Knapsack ks=new Knapsack();  
  33.         System.out.println(ks.Knapsacountk(list.get(1)-1,list.get(0)));  
  34.         ks.print();  
  35.         //写入txt  
  36.         ks.write(result);  
  37.           
  38.     }  
  39.       
  40.     //控制台打印  
  41.     public void print(){  
  42.         for(int i=0;i<result.length;i++)  
  43.         {  
  44.             System.out.println(result[i]);  
  45.         }  
  46.     }  
  47.     //写入txt  
  48.     public  void write(int[] list) {  
  49.         File f = new File("txt/KnapsackResult.txt");  
  50.         FileOutputStream fou = null;  
  51.         try {  
  52.             fou = new FileOutputStream(f, true);// true,设置可追加  
  53.             for (int i = 0; i < list.length; i++) {  
  54.                 String s = String.valueOf(list[i]);  
  55.                 String a = "" + s + "\t\n";  
  56.                 // byte []bytes=new byte[1024];  
  57.                 // 如何把string转换byte数组  
  58.                 fou.write(a.getBytes());  
  59.   
  60.             }  
  61.         } catch (Exception e) {  
  62.             // TODO: handle exception  
  63.             e.printStackTrace();  
  64.         } finally {  
  65.             try {  
  66.                 fou.close();  
  67.             } catch (Exception e) {  
  68.                 // TODO Auto-generated catch block  
  69.                 e.printStackTrace();  
  70.             }  
  71.         }  
  72.     }  
  73.     public int Knapsacountk(int n ,int count)  
  74.     {  
  75.         if(n==-1||count==0)  
  76.             return 0;  
  77.         int tmp1=Knapsacountk(n-1,count);  
  78.         if(weight[n]>count)  
  79.         {  
  80.             result[n]=0;  
  81.             return tmp1;  
  82.         }  
  83.         int tmp2=value[n]+Knapsacountk(n-1,count-weight[n]);  
  84.         if(tmp1>tmp2)  
  85.         {  
  86.             result[n]=0;  
  87.             return tmp1;  
  88.         }  
  89.         result[n]=1;  
  90.         return tmp2;      
  91.     }  
  92.     // 读取文件到Arraylist 数组  
  93.             public static ArrayList read(String path) {  
  94.                 ArrayList<Integer> list = new ArrayList<Integer>();  
  95.                 BufferedReader input = null;  
  96.                 try {  
  97.                     FileReader in = new FileReader(path);  
  98.                     input = new BufferedReader(in);  
  99.                     String ss;  
  100.                     try {  
  101.                         while ((ss = input.readLine()) != null) {  
  102.                             String[] s = (ss.split("  "));  
  103.                             for (int i = 0; i < s.length; i++) {  
  104.                                 list.add(Integer.parseInt(s[i].trim())); // 将String  
  105.                                                                             // s中的内容添加到动态数组中  
  106.                             }  
  107.   
  108.                         }  
  109.                     } catch (IOException e) {  
  110.                         // TODO 自动生成的 catch 块  
  111.                         e.printStackTrace();  
  112.                     }  
  113.                     in.close();  
  114.                     input.close();  
  115.                 } catch (Exception e) {  
  116.                     // TODO 自动生成的 catch 块  
  117.                     e.printStackTrace();  
  118.                 }  
  119.   
  120.                 return list;  
  121.             }  
  122. }  
txt结果:


0 0
原创粉丝点击