Advertisement
Vankata17

GreedyAlgorithm

Nov 6th, 2020 (edited)
2,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class GreedyAlgorithm {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.         int sum = Integer.parseInt(scan.nextLine());
  9.  
  10.         CountNominees(sum);
  11.  
  12.  
  13.     }
  14.  
  15.     private static void CountNominees(int sum) {
  16.         int counter = 0;
  17.         int index = 0;
  18.         int[] nominees = {50, 20, 10,};
  19.  
  20.         while (sum > 0) {
  21.             if (nominees[index] <= sum){
  22.                 sum = sum - nominees[index];
  23.                 counter++;
  24.                 System.out.println(sum + nominees[index] + (" - " + nominees[index] + ";") + " COUNTER IS:" + counter);
  25.  
  26.             } else if (index + 1 < nominees.length) {
  27.                     index++;
  28.                 } else {
  29.                     System.out.println("NO MORE NOMINEES FOR THIS SUM :" + sum);
  30.                     break;
  31.                 }
  32.             }
  33.  
  34.         }
  35.  
  36.  
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement