Zeshin

Tema07GreedyAlgorithm

Nov 8th, 2020 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.util.*;
  2. //Denis 18110
  3. public class Greed {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         int[] nominali = {1,2,5,10,20,50};
  7.         System.out.println("Kolko leva e sumata?"); // Не бях сигурен дали сумата трябва да е във стотинки или в лева.
  8.         double SUM_IN_LEV = scan.nextDouble();
  9.         int SUM_IN_COINS = (int) (SUM_IN_LEV*100);
  10.         int count = greedy(SUM_IN_COINS, nominali);
  11.         System.out.println("Moje da q platite s minimum "+ count + " moneti");
  12.     }
  13.     public static int greedy(int sum, int[] nominali){
  14.         int count = 0;
  15.         for(int i = nominali.length - 1; i>=0; i--){
  16.             while (sum >= nominali[i]) {
  17.                 sum-=nominali[i];
  18.                 count++;
  19.             }
  20.             if(sum ==0){
  21.                 break;
  22.             }
  23.         }
  24.         return count;
  25.     }
  26. }
  27.  
Add Comment
Please, Sign In to add comment