VENDING MACHINE PROBLEM (GREEDY SOLUTION)
namespace ChangeProgram
{
class Program
{
static void DisplayReturnChange(Decimal Paid, Decimal Cost)
{
if (Cost > Paid) return;
if (Cost == Paid)
{
Console.WriteLine("No Change");
return;
}
Decimal ReturnAmt = Paid - Cost;
int Hundreds = 0;
int Twentys = 0;
int Tens = 0;
int Fives = 0;
int Ones = 0;
int Quarters = 0;
Hundreds = (int)ReturnAmt / 100;
ReturnAmt %= 100;
Twentys = (int)ReturnAmt / 20;
ReturnAmt %= 20;
Tens = (int)ReturnAmt / 10;
ReturnAmt %= 10;
Fives = (int)ReturnAmt / 5;
ReturnAmt %= 5;
Ones = (int)ReturnAmt / 1;
ReturnAmt %= 1;
Quarters = (int)(ReturnAmt / (Decimal)0.25);
ReturnAmt %= (Decimal)0.25;
Console.WriteLine(Hundreds + " " + Twentys + " " + Tens + " " + Fives + " " + Ones + " " + Quarters + " " + ReturnAmt);
}
static void Main(string[] args)
{
DisplayReturnChange(334.58M, 234.57M);
}
}
}
No comments:
Post a Comment