Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class BackpackExe {
- public static boolean STATZero = false;
- public static int Iterations = 0;
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- //DECLARING THE VARIABLES//
- System.out.println("Enter backpack capacity(kg): ");
- int M = scan.nextInt();
- System.out.println("Enter number of items(between 2 and 5) :");
- int N = scan.nextInt();
- int[] G = new int[N]; //weight//
- int[] CI = new int[N]; //value//
- //int sum = 0;
- //FILLING THE ARRAY G[N](items, kg)//
- for (int i = 0; i < N; i++) {
- System.out.printf("Please, enter kilos for item [%d]: ", +i);
- int kilos = scan.nextInt();
- if (kilos >= 2) {
- G[i] = kilos;
- } else {
- System.out.println("This item is less than 2kgs!");
- System.out.printf("Please input kilos for item [%d]: ", i);
- kilos = scan.nextInt();
- if (kilos >= 2) {
- G[i] = kilos;
- } else {
- System.out.println("Incorrect value. Try again!");
- System.exit(0);
- }
- }
- }
- //DESCENDING INSERTION SORT FOR G[N]//
- for (int i = 1; i < G.length; i++) {
- int temp = G[i];
- int j = i - 1;
- while (j >= 0 && temp > G[j]) {
- G[j + 1] = G[j];
- --j;
- }
- G[j + 1] = temp;
- }
- ChekBackpack(M, G);
- }
- private static void ChekBackpack(int M, int[] G){
- Scanner PAUSE = new Scanner(System.in);
- int STAT[] = new int[G.length];
- int index = 0;
- int StartInterIndex = 0;
- boolean EOP = false;
- while(M > 0 && !EOP){
- if(G[index] <= M){
- M = M - G[index];
- STAT[index] = STAT[index] + 1;
- STATZero = false;
- System.out.println();
- System.out.println("=================Iteration nomber: " + ++Iterations + " =================");
- System.out.println("The index of this item is: " + index);
- System.out.println("The weight of this item is: " + G[index]);
- System.out.println("In the backpack there is( "+ (M + G[index]) + "-" + G[index] +" )kgs free space");
- System.out.println("STAT-Array[num] " + Arrays.toString(STAT));
- System.out.println("G-Array[kgs]: " + Arrays.toString(G));
- }else{
- System.out.println();
- System.out.println("=================Iteration number: " + ++Iterations + " =================");
- System.out.println("The index of this item is: " + index);
- System.out.println("The weight of this item is: " + G[index]);
- System.out.println("This item is: " + G[index] +
- "kgs more then the the free" + M + "kgs space in the backpack.");
- System.out.println("STAT-Array[num]: " + Arrays.toString(STAT));
- System.out.println("G-Array [kgs]: " + Arrays.toString(G));
- System.out.println();
- if(index + 1 < G.length){
- System.out.println("Moving to the next item ----> ");
- System.out.println("Press <Y> to continue!");
- String pause = PAUSE.next("y");
- index++;
- System.out.println("There is " + M + "kgs free space in the backpack.");
- }else{
- System.out.println("\\^o^/ This is the end of the Array with the items`es weight. \\^o^/");
- System.out.println();
- System.out.println("The index ot the last item is: " + index);
- System.out.println("The weight of this item is: " + G[index]);
- System.out.println("There is " + M + "kgs free space in the backpack.");
- System.out.println("STAT-Array[num]: " + Arrays.toString(STAT));
- System.out.println("G-Array [kgs]: " + Arrays.toString(G));
- System.out.println("Press <Y> to continue!");
- String pause = PAUSE.next("y");
- System.out.println("( ̄︶ ̄)↗ Subtracting the backwards item iterations. ( ̄︶ ̄)↗");
- if(M > 0 && index == G.length - 1){
- for(int j = index; j >= 0; j--){
- if(STAT[j] > 0){
- System.out.println();
- System.out.println("Subtracting one item backwards.");
- System.out.println("Press <Y> to continue!");
- pause = PAUSE.next("y");
- STAT[j]++;
- M = M + G[j];
- if(j == G.length-1 && STAT[j] > 0){
- EOP = true;
- }
- System.out.println("STAT-Array[num]: " + Arrays.toString(STAT));
- System.out.println("G-Array [kgs]: " + Arrays.toString(G));
- System.out.println("Subtracting 1 item from " + G[j] + " kgs.");
- System.out.println("Remaining free space in the backpack " + M);
- System.out.println("Press <Y> to continue!");
- pause = PAUSE.next("y");
- if(StartInterIndex + j + 1 < G.length){
- StartInterIndex = j + 1;
- index = StartInterIndex;
- }else{
- index = G.length - 1;
- }
- }
- }
- }
- }
- }
- }
- if(M == 0){
- System.out.println();
- System.out.println("==============================================");
- System.out.println(" There is solution to the problem! ");
- System.out.println("==============================================");
- System.out.println("There is " + M + "kgs free space in the backpack.");
- System.out.println("STAT-Array[num]: " + Arrays.toString(STAT));
- System.out.println("G-Array [kgs]: " + Arrays.toString(G));
- }else{
- System.out.println();
- System.out.println("==============================================");
- System.out.println(" There is no solution to the problem! ");
- System.out.println("==============================================");
- System.out.println("There is " + M + "kgs free space in the backpack.");
- System.out.println("STAT-Array[num]: " + Arrays.toString(STAT));
- System.out.println("G-Array [kgs]: " + Arrays.toString(G));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement