Advertisement
teddy-popova

Задача за раница

Nov 29th, 2020
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void CheckBackpack(int M, int[] G, int N) {
  4.     int useOfItems[] = new int[N];
  5.         int curSum = M;
  6.  
  7.         for (int i = 0; i < N; i++) {
  8.             useOfItems[i] = curSum / G[i];
  9.             if (curSum - useOfItems[i] * G[i] != 1) {
  10.                 curSum -= useOfItems[i] * G[i];
  11.             }
  12.         }
  13.  
  14.         if (curSum == 0) {
  15.             System.out.println("Намерено е решение! :)");
  16.         } else {
  17.             System.out.println("Не е намерено решение! :(");
  18.         }
  19.     }
  20.  
  21.     public static void main(String[] args) {
  22.         Scanner scan = new Scanner(System.in);
  23.         System.out.println("Въведи вместимостта на раницата/кг/: ");
  24.         int M = scan.nextInt();
  25.         System.out.println("Въведи броя на предметите/между 2 и 5/: ");
  26.         int N = scan.nextInt();
  27.         int[] G = new int[N];
  28.         System.out.println("Въведи теглото на предметите/не по-малко от 2кг/: ");
  29.         for (int i = 0; i < N; i++) {
  30.             int e = scan.nextInt();
  31.             G [i] = e;
  32.         }
  33.  
  34.         CheckBackpack(M, G, N);
  35.     }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement