Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void CheckBackpack(int M, int[] G, int N) {
- int useOfItems[] = new int[N];
- int curSum = M;
- for (int i = 0; i < N; i++) {
- useOfItems[i] = curSum / G[i];
- if (curSum - useOfItems[i] * G[i] != 1) {
- curSum -= useOfItems[i] * G[i];
- }
- }
- if (curSum == 0) {
- System.out.println("Намерено е решение! :)");
- } else {
- System.out.println("Не е намерено решение! :(");
- }
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.println("Въведи вместимостта на раницата/кг/: ");
- int M = scan.nextInt();
- System.out.println("Въведи броя на предметите/между 2 и 5/: ");
- int N = scan.nextInt();
- int[] G = new int[N];
- System.out.println("Въведи теглото на предметите/не по-малко от 2кг/: ");
- for (int i = 0; i < N; i++) {
- int e = scan.nextInt();
- G [i] = e;
- }
- CheckBackpack(M, G, N);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement