Minimal Coin Output 11-02-2013, 03:51 AM
#1
I wrote this for a challenge on another forum. I thought it may be worth while sharing here too.
Here's my solution in C++:
Modulo or the remainder operator for this challenge isn't really necessary anyways. The thing about my solution is it allows you to choose what coins are valid for choice; adaptable/manipulable.
Example output:
I guess I should explain the point of the code, or just the challenge requirements. The idea is to, from a set of choose-able coins in my case, although the challenge was only for a constant collection of quarters, dimes, nickels, and pennies, show the value of the input amount, with the minimum number of coins possible.
This obviously means having as many of the larger coin values as possible to create the sum. My solution is adaptable though. You could choose what coin values you want to include from the constant values defined in my code above, or perhaps even define your own custom coin values if you wanted.
Could change it to this as well to allow both double and an integer type of input for the penny amount:
If I didn't go with the incremental method, I would've used fmod() if dealing with floating point, or just the regular modulo operator for integers. Floating point is okay, but not precise. I'd liked to have used a library like GMP if I took this code more seriously.
Be wary of the lack of preciseness with floating point datatypes though.
Enjoy
Here's my solution in C++:
Code:
#include <iostream>
#include <map>
bool keyComp(unsigned lch, unsigned rch) { return lch > rch; }
typedef std::pair < std::string, size_t > coin_def;
typedef std::map<size_t, coin_def, bool(*)(size_t, size_t)> coin_map;
const size_t TOONIE = 200;
const size_t LOONIE = 100;
const size_t QUARTER = 25;
const size_t DIME = 10;
const size_t NICKEL = 5;
const size_t PENNY = 1;
void CoinsRequired(double amt, coin_map& coinMap)
{
size_t pennySum = amt * 100;
for (const auto &it : coinMap)
{
size_t count = 0;
while (pennySum >= count + it.first)
{
count += it.first;
coinMap[it.first].second++;
}
pennySum -= count;
}
}
int main(void)
{
double amt = 2.37;
coin_map coinMap(keyComp);
coinMap.insert(std::pair < size_t, coin_def >(QUARTER, coin_def("Quarter(s)", 0)));
coinMap.insert(std::pair < size_t, coin_def >(DIME, coin_def("Dime(s)", 0)));
coinMap.insert(std::pair < size_t, coin_def >(NICKEL, coin_def("Nickel(s)", 0)));
coinMap.insert(std::pair < size_t, coin_def >(PENNY, coin_def("Penny/Pennies", 0)));
CoinsRequired(amt, coinMap);
// Output resules
std::cout << "Minimum Coin Sums for: $" << amt << std::endl;
for (const auto &it : coinMap)
{
std::cout.width(15);
std::cout << std::left << it.second.first << ": ";
std::cout << std::right << it.second.second << std::endl;
}
}
Modulo or the remainder operator for this challenge isn't really necessary anyways. The thing about my solution is it allows you to choose what coins are valid for choice; adaptable/manipulable.
Example output:
Code:
Minimum Coin Sums for: $2.37
Quarter(s) : 9
Dime(s) : 1
Nickel(s) : 0
Penny/Pennies : 2
I guess I should explain the point of the code, or just the challenge requirements. The idea is to, from a set of choose-able coins in my case, although the challenge was only for a constant collection of quarters, dimes, nickels, and pennies, show the value of the input amount, with the minimum number of coins possible.
This obviously means having as many of the larger coin values as possible to create the sum. My solution is adaptable though. You could choose what coin values you want to include from the constant values defined in my code above, or perhaps even define your own custom coin values if you wanted.
Could change it to this as well to allow both double and an integer type of input for the penny amount:
Code:
template <typename T>
void CoinsRequired(T amt, coin_map& coinMap)
{
T pennySum = amt;
for (const auto &it : coinMap)
{
T count = 0;
while (pennySum >= count + it.first)
{
count += it.first;
coinMap[it.first].second++;
}
pennySum -= count;
}
}
void CoinsRequired(double amt, coin_map& coinMap)
{
CoinsRequired((size_t) amt * 100, coinMap);
}
If I didn't go with the incremental method, I would've used fmod() if dealing with floating point, or just the regular modulo operator for integers. Floating point is okay, but not precise. I'd liked to have used a library like GMP if I took this code more seriously.
Be wary of the lack of preciseness with floating point datatypes though.
Enjoy
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]