Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import BackPack.BackPack;
  2. import BackPack.Item;
  3.  
  4. import java.util.Vector;
  5.  
  6. public class main {
  7. public static void main(String[] argc){
  8. Vector<Item> listOfItems = new Vector<>();
  9. Item it = new Item(10,100,"Notebook");
  10. Item it2 = new Item(15,1500,"Notebook2");
  11. Item it3 = new Item(1000,100,"Notebook3");
  12. Item it4 = new Item(12000,10000,"Notebook4");
  13. Item it5 = new Item(10,12,"Notebook5");
  14. listOfItems.add(it);
  15. listOfItems.add(it2);
  16. listOfItems.add(it3);
  17. listOfItems.add(it4);
  18. listOfItems.add(it5);
  19. BackPack BP = new BackPack(100);
  20. /*for(int i=0;i<listOfItems.size();i++){
  21. System.out.println(listOfItems.elementAt(i).getCorrelation());
  22. }*/
  23. int[][] solution = new int[listOfItems.size()][(int)BP.getCapacity()];
  24.  
  25. for(int i=0;i<BP.getCapacity();i++){
  26. solution[0][i]=0;
  27. }
  28. for(int i=1;i<listOfItems.size();i++){
  29.  
  30. for (int j = 0; j < BP.getCapacity(); j++) {
  31. if (listOfItems.elementAt(i).getWeight() > j) {
  32. solution[i][j] = solution[i - 1][j];
  33. } else {
  34. solution[i][j] = Math.max(solution[i - 1][j], solution[i - 1]
  35. [j - (int) listOfItems.elementAt(i).getWeight()] + (int) listOfItems.elementAt(i).getCost());
  36. }
  37. }
  38.  
  39. }
  40.  
  41. for( int i=0;i<listOfItems.size();i++){
  42. for (int j=0;j<BP.getCapacity();j++){
  43. System.out.print(solution[i][j]+" ");
  44. }
  45. System.out.println();
  46. }
  47.  
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement