Boyan5

Greedy Algorithm

Nov 12th, 2020 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. import java.util.Scanner;
  2. class domashno {
  3.  
  4.     public static void greedyAlg(int sum){
  5.         //point 1 define arr with nominee and index for massive
  6.         int[] nominee = {50,20,10,5,2,1};
  7.         int index = 0;
  8.         int counter = 0;
  9.  
  10.         while(sum > 0){
  11.  
  12.             if(sum / nominee[index] > 0){
  13.                 sum = sum - nominee[index];
  14.                 counter++;
  15.                 System.out.println("Sum is " + sum + "nominee -> " + nominee[index] + "counter -> " + counter );
  16.  
  17.             }else{
  18.                 index++;
  19.             }
  20.  
  21.         }
  22.  
  23.         System.out.print(counter);
  24.     }
  25.  
  26.  
  27.     public static void main(String[] args) {
  28.         Scanner scanner = new Scanner(System.in);
  29.  
  30.         //point 1 define user inputted money capacity
  31.         System.out.print("Enter money nominee: ");
  32.  
  33.         int sum = scanner.nextInt();
  34.  
  35.         greedyAlg(sum);
  36.  
  37.     }
  38. }
  39.  
Add Comment
Please, Sign In to add comment