Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class greedy {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.print("Input sum: ");
- int Sum = input.nextInt();
- CountNominees(Sum);
- }// end of main
- private static void CountNominees(int sum) {
- int counter = 0;// count the coins
- int index = 0;//index of nominees
- int[] nominees = {50,20,10,5,2,1};
- while(sum > 0){
- if (nominees[index]<= sum){
- sum = sum - nominees[index];
- counter++;
- System.out.println(sum + nominees[index] + "(-" + nominees[index] + "); Counter : " + counter);
- }
- else{
- if (index + 1 < nominees.length) {
- index++;
- }
- else{
- System.out.println("There is no other nominees for this Sum: " + sum);
- break;
- }
- }
- }//end of while
- System.out.println("Number of coins is : " + counter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement