Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void check(int m, int[] weight, int n) {
- int useOfItems[] = new int[n];
- int curSum = m;
- for (int i = 0; i < n; i++) {
- useOfItems[i] = curSum / weight[i];
- if (curSum - useOfItems[i] * weight[i] != 1) {
- curSum -= useOfItems[i] * weight[i];
- }
- }
- if (curSum == 0) {
- System.out.println("A solution is found");
- } else {
- System.out.println("A solution is not found");
- }
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.println("Input the capacity: ");
- int m = scan.nextInt();
- System.out.println("Input the number of items: ");
- int n = scan.nextInt();
- int[] weight = new int[n];
- System.out.println("Input the weight of the items: ");
- for (int i = 0; i < n; i++) {
- int e = scan.nextInt();
- weight[i] = e;
- }
- check(m, weight, n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement