Geeks With Blogs
They call me Big Papa 4 kids and counting

Given an arbitrary number of coins to use, print out a combination using that number that will add up to a dollar.
For instance, using the number 4:
Q : 4
D : 0
N : 0
P : 0
If the number is invalid, such as 5:
Invalid Number

- The method must take an Integer value with no restrictions.
- All data structures must be instantiated within the method.
- Only Pennies, Nickels, Dimes, and Quarters can be used. (No Half Dollars)
- The lowest IL wins.


One solution (103 IL)

static void Amount(int total)
{
bool invalid = true;
for (int quarters = 0; quarters <= 4; quarters++)
{
for (int dimes = 0; dimes <= 10; dimes++)
{
for (int nickels = 0; nickels <= 20; nickels++)
{
for (int pennies = 0; pennies <= 100; pennies++)
{
if ((quarters*25 + dimes * 10 + nickels * 5 + pennies == 100) &&
(total == quarters + dimes + nickels + pennies))
{
Console.WriteLine("Q: " + quarters);
Console.WriteLine("D: " + dimes);
Console.WriteLine("N: " + nickels);
Console.WriteLine("P: " + pennies);
invalid = false;
}


Winner (84 IL):

void getADollar(int coinCount)
{
int d, n, p, q = 0;
bool solution = true;
do
{
d = 0;
do
{
n = 0;
do
{
p = coinCount - q - d - n;
if ((((q * 25) + (d * 10) + (n * 5) + p) == 100) && p >= 0)
{
Console.WriteLine(String.Format("Q : {0}\nD : {1}\nN : {2}\nP : " + p, q, d, n));
solution = false;
}
n++;
} while (n <= coinCount);
d++;
} while (d <= coinCount);
q++;
} while (q <= coinCount);
if (solution)
Console.WriteLine("Invalid Number");
}

Posted on Monday, October 1, 2007 8:07 AM Coding Challenge | Back to top


Comments on this post: Coding Challenge - Can I get a dollar?

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © baileyrt | Powered by: GeeksWithBlogs.net