Advertisement
Vasilena

ТЕМА 08

Dec 10th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.22 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class BackpackExe {
  5.     public static boolean STATZero = false;
  6.     public static int Iterations = 0;
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner scan = new Scanner(System.in);
  10.         //DECLARING THE VARIABLES//
  11.         System.out.println("Enter backpack capacity(kg): ");
  12.         int M = scan.nextInt();
  13.         System.out.println("Enter number of items(between 2 and 5) :");
  14.         int N = scan.nextInt();
  15.         int[] G = new int[N]; //weight//
  16.         int[] CI = new int[N]; //value//
  17.         //int sum = 0;
  18.  
  19.  
  20.         //FILLING THE ARRAY G[N](items, kg)//
  21.         for (int i = 0; i < N; i++) {
  22.             System.out.printf("Please, enter kilos for item [%d]: ", +i);
  23.             int kilos = scan.nextInt();
  24.             if (kilos >= 2) {
  25.                 G[i] = kilos;
  26.             } else {
  27.                 System.out.println("This item is less than 2kgs!");
  28.                 System.out.printf("Please input kilos for item [%d]: ", i);
  29.                 kilos = scan.nextInt();
  30.                 if (kilos >= 2) {
  31.                     G[i] = kilos;
  32.                 } else {
  33.                     System.out.println("Incorrect value. Try again!");
  34.                     System.exit(0);
  35.                 }
  36.             }
  37.         }
  38.  
  39.         //DESCENDING INSERTION SORT FOR G[N]//
  40.         for (int i = 1; i < G.length; i++) {
  41.             int temp = G[i];
  42.             int j = i - 1;
  43.             while (j >= 0 && temp > G[j]) {
  44.                 G[j + 1] = G[j];
  45.                 --j;
  46.             }
  47.             G[j + 1] = temp;
  48.         }
  49.  
  50.         ChekBackpack(M, G);
  51.     }
  52.  
  53.     private static void ChekBackpack(int M, int[] G){
  54.         Scanner PAUSE = new Scanner(System.in);
  55.  
  56.         int STAT[] = new int[G.length];
  57.         int index = 0;
  58.         int StartInterIndex = 0;
  59.         boolean EOP = false;
  60.  
  61.         while(M > 0 && !EOP){
  62.             if(G[index] <= M){
  63.                 M = M - G[index];
  64.                 STAT[index] = STAT[index] + 1;
  65.                 STATZero = false;
  66.                 System.out.println();
  67.                 System.out.println("=================Iteration nomber: " + ++Iterations + " =================");
  68.                 System.out.println("The index of this item is: " + index);
  69.                 System.out.println("The weight of this item is: " + G[index]);
  70.                 System.out.println("In the backpack there is( "+ (M + G[index]) + "-" + G[index] +" )kgs free space");
  71.                 System.out.println("STAT-Array[num] " + Arrays.toString(STAT));
  72.                 System.out.println("G-Array[kgs]: " + Arrays.toString(G));
  73.             }else{
  74.                 System.out.println();
  75.                 System.out.println("=================Iteration number: " + ++Iterations + " =================");
  76.                 System.out.println("The index of this item is: " + index);
  77.                 System.out.println("The weight of this item is: " + G[index]);
  78.                 System.out.println("This item is: " + G[index] +
  79.                         "kgs more then the the free" + M + "kgs space in the backpack.");
  80.                 System.out.println("STAT-Array[num]: " + Arrays.toString(STAT));
  81.                 System.out.println("G-Array [kgs]: " + Arrays.toString(G));
  82.  
  83.                 System.out.println();
  84.                 if(index + 1 < G.length){
  85.                     System.out.println("Moving to the next item ----> ");
  86.                     System.out.println("Press <Y> to continue!");
  87.                     String pause = PAUSE.next("y");
  88.                     index++;
  89.                     System.out.println("There is " + M + "kgs free space in the backpack.");
  90.                 }else{
  91.                     System.out.println("\\^o^/ This is the end of the Array with the items`es weight. \\^o^/");
  92.                     System.out.println();
  93.                     System.out.println("The index ot the last item is: " + index);
  94.                     System.out.println("The weight of this item is: " + G[index]);
  95.                     System.out.println("There is " + M + "kgs free space in the backpack.");
  96.                     System.out.println("STAT-Array[num]: " + Arrays.toString(STAT));
  97.                     System.out.println("G-Array [kgs]: " + Arrays.toString(G));
  98.  
  99.                     System.out.println("Press <Y> to continue!");
  100.                     String pause = PAUSE.next("y");
  101.  
  102.                     System.out.println("( ̄︶ ̄)↗ Subtracting the backwards item iterations. ( ̄︶ ̄)↗");
  103.                     if(M > 0 && index == G.length - 1){
  104.                         for(int j = index; j >= 0; j--){
  105.                             if(STAT[j] > 0){
  106.                                 System.out.println();
  107.                                 System.out.println("Subtracting one item backwards.");
  108.  
  109.                                 System.out.println("Press <Y> to continue!");
  110.                                 pause = PAUSE.next("y");
  111.  
  112.                                 STAT[j]++;
  113.                                 M = M + G[j];
  114.  
  115.                                 if(j == G.length-1 && STAT[j] > 0){
  116.                                     EOP = true;
  117.                                 }
  118.  
  119.                                 System.out.println("STAT-Array[num]: " + Arrays.toString(STAT));
  120.                                 System.out.println("G-Array [kgs]: " + Arrays.toString(G));
  121.                                 System.out.println("Subtracting 1 item from " + G[j] + " kgs.");
  122.                                 System.out.println("Remaining free space in the backpack " + M);
  123.  
  124.                                 System.out.println("Press <Y> to continue!");
  125.                                 pause = PAUSE.next("y");
  126.  
  127.                                 if(StartInterIndex + j + 1 < G.length){
  128.                                     StartInterIndex = j + 1;
  129.                                     index = StartInterIndex;
  130.                                 }else{
  131.                                     index = G.length - 1;
  132.                                 }
  133.                             }
  134.                         }
  135.                     }
  136.                 }
  137.             }
  138.         }
  139.         if(M == 0){
  140.             System.out.println();
  141.             System.out.println("==============================================");
  142.             System.out.println("      There is solution to the problem!       ");
  143.             System.out.println("==============================================");
  144.             System.out.println("There is " + M + "kgs free space in the backpack.");
  145.             System.out.println("STAT-Array[num]: " + Arrays.toString(STAT));
  146.             System.out.println("G-Array [kgs]: " + Arrays.toString(G));
  147.         }else{
  148.             System.out.println();
  149.             System.out.println("==============================================");
  150.             System.out.println("      There is no solution to the problem!       ");
  151.             System.out.println("==============================================");
  152.             System.out.println("There is " + M + "kgs free space in the backpack.");
  153.             System.out.println("STAT-Array[num]: " + Arrays.toString(STAT));
  154.             System.out.println("G-Array [kgs]: " + Arrays.toString(G));
  155.         }
  156.     }
  157. }
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement