Advertisement
BetinaUKTC

ranica

Dec 16th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void check(int m, int[] weight, int n) {
  5. int useOfItems[] = new int[n];
  6. int curSum = m;
  7.  
  8. for (int i = 0; i < n; i++) {
  9. useOfItems[i] = curSum / weight[i];
  10. if (curSum - useOfItems[i] * weight[i] != 1) {
  11. curSum -= useOfItems[i] * weight[i];
  12. }
  13. }
  14.  
  15. if (curSum == 0) {
  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[] weight = 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. weight[i] = e;
  33. }
  34.  
  35. check(m, weight, n);
  36. }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement