fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. /// Get minimal number of coins used to give out amount using greedy algorithm
  6. /// \param amount - amount to give out
  7. /// \param coins - array of available coins, sorted in descending order
  8. /// \return minimum number of coins from array coins used to give out amount
  9. int changeGreedy(int amount, int coins[]) {
  10. int result = 0;
  11. int i = 0;
  12.  
  13. while (amount > 0) {
  14. result += amount / coins[i];
  15. amount %= coins[i];
  16. i++;
  17. }
  18.  
  19. return result;
  20. }
  21.  
  22. int main() {
  23. int amount, result;
  24. int coins[8] = {200, 100, 50, 20, 10, 5, 2, 1};
  25. amount = 589;
  26.  
  27. result = changeGreedy(amount, coins);
  28.  
  29. cout << "Algorytm zachlanny" << endl;
  30. cout << "Kwota " << amount << " moze zostac wydana przy uzyciu " << result << " monet/banknotow." << endl;
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0s 5552KB
stdin
Standard input is empty
stdout
Algorytm zachlanny
Kwota 589 moze zostac wydana przy uzyciu 9 monet/banknotow.