Advertisement
Dimitar182

ranica

Dec 21st, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. package ranica;
  2. import java.util.Scanner;
  3. import java.util.Arrays;
  4. class homework4 {
  5.  
  6.  
  7. public static void sortWeights(int[] weights){
  8. for(int i = 1; i < weights.length;i++){
  9. int getedEl = weights[i];
  10. int lastSorted = i - 1;
  11. while(lastSorted >= 0 && weights[lastSorted] < getedEl){
  12. weights[lastSorted + 1] = weights[lastSorted];
  13. lastSorted --;
  14. }
  15. weights[lastSorted + 1] = getedEl;
  16. }
  17. }
  18. public static void bagPackageCounterCheck(int capacity, int[] weights) {
  19. int counter = 0;
  20. int index = 0;
  21. while(capacity > 0){
  22. if(index < weights.length){
  23. if(capacity / weights[index] > 0){
  24. capacity -= weights[index];
  25. counter++;
  26. System.out.println("Capacity is " + capacity + " Weight -> " + weights[index] + " Counter -> " + counter );
  27. }else{
  28. index++;
  29. }
  30. }else{
  31. System.out.print("The backpack cannot be filled with these packages!Rest is :" + capacity);
  32. break;
  33. }
  34. }
  35. }
  36. public static void main(String[] args) {
  37. Scanner scanner = new Scanner(System.in);
  38. System.out.print("Enter bagpack capacity --> ");
  39. int bagCapacity = scanner.nextInt();
  40. System.out.print("How many items you have for bagpack (2-5) --> ");
  41. int items = scanner.nextInt();
  42. int[] itemWeight = new int[items];
  43. while(items < 2 || items > 5 ){
  44. System.out.print("Enter valid number -- > ");
  45. items = scanner.nextInt();
  46. }
  47. for(int i = 0; i < items;i++){
  48. System.out.printf("How many kilograms do the items wheight? [%d] :",i);
  49. itemWeight[i] = scanner.nextInt();
  50. }
  51. sortWeights(itemWeight);
  52. bagPackageCounterCheck(bagCapacity , itemWeight);
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement