Greedy Algorithm

Hi I was recently trying to create a greedy algorithm and found good tips and source code on this site and so I thought that I would display this source code not as my own but for everyone to be able to study and test. Dont feel bad about having to look at someone elses source code because your stuck because this is all just a learning experience; so if your stuck you will now know how to create a simple greedy algorithm and now will most likely be able to create one by yourself.

#include <iostream>

using namespace std;
int main()
{
const int quat=25;
const int dime=10;
const int nick=5;
const int penn=1;
int change;
cout << "How much change is due in cents: \n";
cin >> change;
cout << change / quat << " quarters are due.\n";
change=(change%quat);

cout << change/dime << " dimes are due.\n";
change=(change%dime);


cout << change/nick << " nickels are due.\n";
change=(change%nick);

cout << change/penn << " pennies are due.\n";
change=(change%penn);
system("pause");
return 0;
}
closed account (3TXyhbRD)
Use code tags and indent.
A bit useless to offer a greedy algorithm without describing the problem. Also, greedy algorithms are very problem-specific, so looking at this one while trying to make your own for a different problem is entirely useless.
Topic archived. No new replies allowed.