minCoinExchange

You are given a set of coins {1, 5, 10, 25}, and an amount. Design a greedy algorithm that returns the minimum number of coins necessary to make up for this amount. For example, assume you have the coins given above, and the amount is 68. Then the minimum number of coins necessary to make up for this amount is:
Two 25 coins
One 10 coin
One 5 coin
Three 1 coins
Total: 7 coins (2x25+1x10+1x5+3x1 = 68)

#include <stdio.h>
#include
#include

using namespace std;

extern int MinCoinExchange(vector &coins, int amount);

///-----------------------------------------------------
/// Test1
///
int Q1Test1() {
vector coins = { 5, 1, 25, 10 };
int amount = 68;

int noCoins = MinCoinExchange(coins, amount);
if (noCoins == 7) return 50;

return 0;

} //end-Q1Test1

///-----------------------------------------------------
/// Test2
///
int Q1Test2() {
vector coins = { 5, 1, 50, 25, 10 };
int amount = 77;

int noCoins = MinCoinExchange(coins, amount);
if (noCoins == 4) return 50;

return 0;

} //end-Q1Test2

///------------------------------------------------------
/// Test for MinCoinExchange
///
int Q1Test() {
int grade = 0;

grade += Q1Test1();
grade += Q1Test2();

return grade;

} //end-Q1Test

Comments (0)