第七周作业—背包问题

  1. package algorithm;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileReader;  
  6. import java.io.FileWriter;  
  7. import java.util.StringTokenizer;  
  8.   
  9. public class Knapsack {  
  10.     private static int MAX_WEIGHT;  
  11.     private static int NUM;  
  12.     private static int[][] data;  
  13.     private static int[][] goods;  
  14.   
  15.     private static void getGoodsData() {  
  16.         BufferedReader reader;  
  17.   
  18.         try {  
  19.             File file = new File("src/dataFile/Knapsack.txt");  
  20.             reader = new BufferedReader(new FileReader(file));  
  21.   
  22.             StringTokenizer tokenizer = new StringTokenizer(reader.readLine()  
  23.                     .trim());  
  24.             MAX_WEIGHT = Integer.valueOf(tokenizer.nextToken());  
  25.             NUM = Integer.valueOf(tokenizer.nextToken());  
  26.   
  27.             goods = new int[NUM][2];  
  28.   
  29.             String temp = null;  
  30.             int currentIndex = 0;  
  31.             while ((temp = reader.readLine()) != null) {  
  32.                 tokenizer = new StringTokenizer(temp);  
  33.                 int index = 0;  
  34.   
  35.                 while (tokenizer.hasMoreElements()) {  
  36.                     goods[currentIndex][index] = Integer.valueOf(tokenizer  
  37.                             .nextToken());  
  38.                     index++;  
  39.                 }  
  40.   
  41.                 currentIndex++;  
  42.             }  
  43.         } catch (Exception e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47.   
  48.     private static void initData() {  
  49.         data = new int[MAX_WEIGHT + 1][NUM + 1];  
  50.         for (int i = 0; i < MAX_WEIGHT + 1; i++) {  
  51.             for (int j = 0; j < NUM + 1; j++) {  
  52.                 data[i][j] = 0;  
  53.             }  
  54.         }  
  55.     }  
  56.   
  57.     public static void calculate() {  
  58.         boolean isTaked[] = new boolean[5];  
  59.         for (int i = 0; i < 5; i++) {  
  60.             isTaked[i] = false;  
  61.         }  
  62.   
  63.         for (int j = 1; j <= NUM; j++) {  
  64.             for (int w = 1; w <= MAX_WEIGHT; w++) {  
  65.                 if (goods[j - 1][0] > w) {  
  66.                     data[w][j] = data[w][j - 1];  
  67.                 } else {              
  68.                     data[w][j] = getMax(data[w][j - 1], data[w - goods[j - 1][0]][j - 1] + goods[j - 1][1]);  
  69.                 }  
  70.             }  
  71.         }  
  72.     }  
  73.   
  74.     private static int getMax(int x, int y) {  
  75.         if (x > y) {  
  76.             return x;  
  77.         } else {  
  78.             return y;  
  79.         }  
  80.     }  
  81.       
  82.     private static void write2File() {  
  83.         FileWriter output = null;  
  84.         try {  
  85.             output = new FileWriter(new File("src/dataFile/KnapsackResult.txt"));  
  86.               
  87.             for (int i=0; i<data.length; i++) {  
  88.                 for (int j=0; j<data[i].length; j++) {  
  89.                     output.write(data[i][j] + "\t\t");  
  90.                 }  
  91.                 output.write("\n");  
  92.             }  
  93.               
  94.             output.flush();  
  95.         } catch (Exception e) {  
  96.             e.printStackTrace();  
  97.         } finally {  
  98.             try {  
  99.                 output.close();  
  100.             } catch (Exception e) {  
  101.                 e.printStackTrace();  
  102.             }  
  103.         }  
  104.     }  
  105.       
  106.     public static void main(String[] args) {  
  107.         // 获取物品的重量和价值  
  108.         getGoodsData();  
  109.   
  110.         // 初始化data数组表  
  111.         initData();  
  112.   
  113.         //计算二维表  
  114.         calculate();  
  115.           
  116.         //输出结果文件  
  117.         write2File();  
  118.     }  
  119. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值