Advertisement
Petra_Abrasheva_185

Backpack_08_1.1-algoritms

Nov 15th, 2020
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Main {
  3.    public static void check(int m, int[] g) {
  4.        int sum = 0;
  5.        int uo = 0;
  6.        int cs= 0;
  7.        for (int i = 0; i < g.length; i++) {
  8.            uo= (cs/ g[i]) * g[i];
  9.            cs= m - uo;
  10.            if (sum + uo<= m && m - cs + uo!= 1) {
  11.                sum +=uo;
  12.            }
  13.        }
  14.  
  15.        if (sum == m) {
  16.            System.out.println("A solution is found");
  17.        } else {
  18.            System.out.println("A solution is not found");
  19.        }
  20.    }
  21.  
  22.    public static void main(String[] args) {
  23.        Scanner scan = new Scanner(System.in);
  24.        System.out.println("Input the capacity: " );
  25.        int m = scan.nextInt();
  26.        System.out.println("Input the number of items: ");
  27.        int n = scan.nextInt();
  28.        int[] g = new int[n];
  29.        System.out.println("Input the weight of the items: ");
  30.        for (int i = 0; i < n; i++) {
  31.            int e = scan.nextInt();
  32.            g[i] = e;
  33.        }
  34.  
  35.        check(m, g);
  36.    }
  37. }
  38.  
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement