Guest User

Untitled

a guest
Nov 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define COIN_ARRAY_SIZE 3
  4.  
  5.  
  6. int get_highest_value_coin_for(int target_value){
  7. int coins[COIN_ARRAY_SIZE] = {10, 5, 1};
  8. int index_of_highest_value_coin;
  9.  
  10. for (size_t i = 0; i < COIN_ARRAY_SIZE ; i++){
  11. if (coins[i] <= target_value){
  12. index_of_highest_value_coin = i;
  13. break;
  14. }
  15. }
  16. return coins[index_of_highest_value_coin];
  17. }
  18.  
  19. int get_change(int target_value) {
  20. int coin_value_to_use;
  21. int number_coins_to_take;
  22. int total_number_coins = 0;
  23. while (target_value) {
  24. coin_value_to_use = get_highest_value_coin_for(target_value);
  25. number_coins_to_take = target_value / coin_value_to_use;
  26. target_value -= number_coins_to_take * coin_value_to_use;
  27. total_number_coins += number_coins_to_take;
  28. }
  29. return total_number_coins;
  30. }
  31.  
  32. int main() {
  33. int target_value;
  34. std::cin >> target_value;
  35. std::cout << get_change(target_value) << '\n';
  36. }
Add Comment
Please, Sign In to add comment