Advertisement
Boyan5

Greedy Algorithm 7 topic

Nov 13th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class greedy {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner input = new Scanner(System.in);
  7.         System.out.print("Input sum: ");
  8.         int Sum = input.nextInt();
  9.  
  10.         CountNominees(Sum);
  11.     }// end of main
  12.  
  13.     private static void CountNominees(int sum) {
  14.       int counter = 0;// count the coins
  15.       int index = 0;//index of nominees
  16.       int[] nominees = {50,20,10,5,2,1};
  17.  
  18.       while(sum > 0){
  19.           if (nominees[index]<= sum){
  20.               sum = sum - nominees[index];
  21.               counter++;
  22.               System.out.println(sum + nominees[index] + "(-" + nominees[index] + "); Counter : " + counter);
  23.           }
  24. else{
  25.     if (index + 1 < nominees.length) {
  26.         index++;
  27.     }
  28. else{
  29.               System.out.println("There is no other nominees for this Sum: " + sum);
  30.               break;
  31.           }
  32.           }
  33.  
  34.       }//end of while
  35.         System.out.println("Number of coins is : " + counter);
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement