0-1背包 - 其他输入值不适用于程序

时间:2012-12-13 16:04:12

标签: java output knapsack-problem

我需要做的是实现0-1背包问题。有一个名为“In0302.txt”的输入文件和一个名为“Out0302.txt”的输出文件。程序从INPUT文件获取值并将结果保存到OUTPUT文件。我从“dr”中得到了纸上的输入值。 将它们放到文件中似乎可以在OUTPUT文件中使用。但是......在类“dr”上尝试将其他值放在INPUT文件中,但该程序不起作用。更有甚至没有错误,但程序仍在编译和编译......我无法知道问题出在哪里。是否有人会尝试更改此代码中的内容或告诉我出了什么问题?

INPUT:
4 6
2 1
3 2
3 4
4 5

OUTPUT:
1 4
2 3

JAVA CODE:

public class Knapsack {
  public static int max(int a, int b){
    if (a > b){
      return a;
    }
    else{
      return b;
    }  
  }
  /**
   * @param args the command line arguments
   */

  public static void main(String[] args){
    int n = 0, W = 0, p[] = null, w[] = null, S[][] = null, S_object[][] = null;
    //s[][] = arrays with values

    try {
      FileReader fr = new FileReader("In0302.txt");
      BufferedReader in = new BufferedReader(fr);
      String line = in.readLine();
      String[] cols = line.split(" ");

      n = Integer.parseInt(cols[0]); W = Integer.parseInt(cols[1]); 

      p = new int[n+1]; w = new int[n+1];

      int k = 1;

      while ((line = in.readLine()) != null) {
        cols = line.split(" ");
        p[k] = Integer.parseInt(cols[0]);
        w[k] = Integer.parseInt(cols[1]);
        k++;
      }

      S = new int[W+1][n+1];
      S_object = new int[W+1][n+1];

      for(int weight = 0; weight <= W; weight++){
        for(int i = 0; i <= n; i++){
          if (i == 0){
            S[weight][i] = 0;
            S_object[weight][i] = 0;
          }
          else if (weight < w[i]){
            S[weight][i] = S[weight][i-1];
            S_object[weight][i] = S_object[weight][i-1];
          }
          else if (weight >= w[i-1]){
            S[weight][i]=max(S[weight][i-1],S[weight-w[i]][i-1]+p[i]);

            if ((max(S[weight][i-1], S[weight-w[i]][i-1] + p[i]) == (S[weight-w[i]][i-1] + p[i])))       {
              S_object[weight][i]=i;
            } //added new element to bag
            else {
              S_object[weight][i]=S_object[weight][i-1];
            } //nothing has been added
          }  
        }
      }
      in.close();
      fr.close(); 
    }

    catch (IOException e){
      System.out.println("Error: " + e.toString());
    }   

    File outputFile;
    FileWriter out;

    try{
      outputFile = new File("Out0302.txt");
      out = new FileWriter(outputFile);
      String line = "";

      int max_value = S[W][n];
      for (int m = n; m > 0; m--){
        if (S[W][m] == max_value){
          line = " " + S_object[W][m] + "";
          int temp = W;

          while ((temp-w[S_object[temp][m]]) > 0){
            temp = temp - w[S_object[temp][m]];
            line += " " + S_object[temp][m];
          }
          out.write(line + "\n");
          out.write("\n\t");
        }
      } 

      out.close();
    } 
    catch (IOException e) {
      System.out.println("Error: " + e.toString());
    }

    System.out.println("Arrar of values:");
    for(int weight = 0; weight <= W; weight++){
      for(int i = 0; i <= n; i++){
        System.out.print(S[weight][i] + " ");
      }
      System.out.println(""); 
    }

    System.out.println("Array of objects:");
    for(int weight = 0; weight <= W; weight++){
      for(int i = 0; i <= n; i++){
        System.out.print(S_object[weight][i] + " ");
      }
      System.out.println("");
    }
  }  
}

0 个答案:

没有答案