a thoughtful web.
Good ideas and conversation. No ads, no tracking.   Login or Take a Tour!
comment by briandmyers
briandmyers  ·  3373 days ago  ·  link  ·    ·  parent  ·  post: A nice problem from SICP

I banged out a brute-force iterative solution pretty quickly in C.

It's not general, but could be made so pretty easily.

  #include <stdio.h>

#define NUM_COINS 5

#define GOAL_IN_CENTS 100

unsigned char ucCoinVal[] = { 1, 5, 10, 25, 50 };

unsigned char isGoodSet( unsigned char aucCoins[] );

int main( void )

{

int i;

unsigned char max[NUM_COINS];

unsigned char aucCoins[NUM_COINS];

int iTotal = 0;

for ( i = 0; i < NUM_COINS; i++ )

{

max[i] = GOAL_IN_CENTS / ucCoinVal[i];

printf( "\nCoin %3d cents has max %3d for goal", ucCoinVal[i], max[i] );

}

printf( "\n" );

for ( aucCoins[0] = 0; aucCoins[0] <= max[0]; aucCoins[0]++ )

{

for ( aucCoins[1] = 0; aucCoins[1] <= max[1]; aucCoins[1]++ )

{

for ( aucCoins[2] = 0; aucCoins[2] <= max[2]; aucCoins[2]++ )

{

for ( aucCoins[3] = 0; aucCoins[3] <= max[3]; aucCoins[3]++ )

{

for ( aucCoins[4] = 0; aucCoins[4] <= max[4]; aucCoins[4]++ )

{

if ( isGoodSet( aucCoins ) )

{

iTotal++;

printf( "\nGood set : %3d %3d %3d %3d %3d",

aucCoins[0], aucCoins[1], aucCoins[2], aucCoins[3], aucCoins[4] );

}

}

}

}

}

}

printf( "\nFound %d solutions.\n", iTotal );

}

// Function to determine if we have a good set of coins (adds up to the goal).

unsigned char isGoodSet( unsigned char aucCoins[] )

{

int i;

int iSum = 0;

for ( i = 0; i < NUM_COINS; i++ )

{

iSum += ( aucCoins[i] * ucCoinVal[i] );

}

return ( iSum == GOAL_IN_CENTS ) ? 1 : 0;

}





dingus  ·  3373 days ago  ·  link  ·  

oh, but brute-forcing it is cheating.

briandmyers  ·  3373 days ago  ·  link  ·  

Nonsense! The rule is always, "Get it working first, then optimise - but only if necessary!"

But there are several simple optimisations possible, I freely admit :-)

briandmyers  ·  3373 days ago  ·  link  ·  

Note - in the centre of the loops, markup has swallowed my increment operator - should be "iTotal ++;"

Same story for all the for-loop incrementors.

There may be other manglings I haven't noticed.

[edit] I've fixed up the missing "++" operators - let me know if you notice any other manglings.

bhrgunatha  ·  3372 days ago  ·  link  ·  

hubski's markdown implementation is really bad for posting code because it still parses what's inside the code rather than outputting it verbatim. You STILL have to escape special symbols ilke + inside code too. It's very frustrating.

insomniasexx  ·  3372 days ago  ·  link  ·  

mk forwardslash rob05c -- regarding our conversation regarding markdown

briandmyers  ·  3372 days ago  ·  link  ·  

Agreed - not great for code, or for poetry / song-lyrics. I had to bung in two spaces at the front of every line to convince it to use a monospace font.

There ought to be a way to say "don't format this text at all please".

briandmyers  ·  3373 days ago  ·  link  ·  

Here's the output (or run it yourself, if you have gcc) :

    Coin 1 cents has max 100 for goal

    Coin 5 cents has max 20 for goal

    Coin 10 cents has max 10 for goal

    Coin 25 cents has max 4 for goal

    Coin 50 cents has max 2 for goal

    Good set : 0 0 0 0 2

    Good set : 0 0 0 2 1

    Good set : 0 0 0 4 0

    Good set : 0 0 5 0 1

    Good set : 0 0 5 2 0

    Good set : 0 0 10 0 0

    Good set : 0 1 2 1 1

    Good set : 0 1 2 3 0

    Good set : 0 1 7 1 0

    Good set : 0 2 4 0 1

    Good set : 0 2 4 2 0

    Good set : 0 2 9 0 0

    Good set : 0 3 1 1 1

    Good set : 0 3 1 3 0

    Good set : 0 3 6 1 0

    Good set : 0 4 3 0 1

    Good set : 0 4 3 2 0

    Good set : 0 4 8 0 0

    Good set : 0 5 0 1 1

    Good set : 0 5 0 3 0

    Good set : 0 5 5 1 0

    Good set : 0 6 2 0 1

    Good set : 0 6 2 2 0

    Good set : 0 6 7 0 0

    Good set : 0 7 4 1 0

    Good set : 0 8 1 0 1

    Good set : 0 8 1 2 0

    Good set : 0 8 6 0 0

    Good set : 0 9 3 1 0

    Good set : 0 10 0 0 1

    Good set : 0 10 0 2 0

    Good set : 0 10 5 0 0

    Good set : 0 11 2 1 0

    Good set : 0 12 4 0 0

    Good set : 0 13 1 1 0

    Good set : 0 14 3 0 0

    Good set : 0 15 0 1 0

    Good set : 0 16 2 0 0

    Good set : 0 18 1 0 0

    Good set : 0 20 0 0 0

    Good set : 5 0 2 1 1

    Good set : 5 0 2 3 0

    Good set : 5 0 7 1 0

    Good set : 5 1 4 0 1

    Good set : 5 1 4 2 0

    Good set : 5 1 9 0 0

    Good set : 5 2 1 1 1

    Good set : 5 2 1 3 0

    Good set : 5 2 6 1 0

    Good set : 5 3 3 0 1

    Good set : 5 3 3 2 0

    Good set : 5 3 8 0 0

    Good set : 5 4 0 1 1

    Good set : 5 4 0 3 0

    Good set : 5 4 5 1 0

    Good set : 5 5 2 0 1

    Good set : 5 5 2 2 0

    Good set : 5 5 7 0 0

    Good set : 5 6 4 1 0

    Good set : 5 7 1 0 1

    Good set : 5 7 1 2 0

    Good set : 5 7 6 0 0

    Good set : 5 8 3 1 0

    Good set : 5 9 0 0 1

    Good set : 5 9 0 2 0

    Good set : 5 9 5 0 0

    Good set : 5 10 2 1 0

    Good set : 5 11 4 0 0

    Good set : 5 12 1 1 0

    Good set : 5 13 3 0 0

    Good set : 5 14 0 1 0

    Good set : 5 15 2 0 0

    Good set : 5 17 1 0 0

    Good set : 5 19 0 0 0

    Good set : 10 0 4 0 1

    Good set : 10 0 4 2 0

    Good set : 10 0 9 0 0

    Good set : 10 1 1 1 1

    Good set : 10 1 1 3 0

    Good set : 10 1 6 1 0

    Good set : 10 2 3 0 1

    Good set : 10 2 3 2 0

    Good set : 10 2 8 0 0

    Good set : 10 3 0 1 1

    Good set : 10 3 0 3 0

    Good set : 10 3 5 1 0

    Good set : 10 4 2 0 1

    Good set : 10 4 2 2 0

    Good set : 10 4 7 0 0

    Good set : 10 5 4 1 0

    Good set : 10 6 1 0 1

    Good set : 10 6 1 2 0

    Good set : 10 6 6 0 0

    Good set : 10 7 3 1 0

    Good set : 10 8 0 0 1

    Good set : 10 8 0 2 0

    Good set : 10 8 5 0 0

    Good set : 10 9 2 1 0

    Good set : 10 10 4 0 0

    Good set : 10 11 1 1 0

    Good set : 10 12 3 0 0

    Good set : 10 13 0 1 0

    Good set : 10 14 2 0 0

    Good set : 10 16 1 0 0

    Good set : 10 18 0 0 0

    Good set : 15 0 1 1 1

    Good set : 15 0 1 3 0

    Good set : 15 0 6 1 0

    Good set : 15 1 3 0 1

    Good set : 15 1 3 2 0

    Good set : 15 1 8 0 0

    Good set : 15 2 0 1 1

    Good set : 15 2 0 3 0

    Good set : 15 2 5 1 0

    Good set : 15 3 2 0 1

    Good set : 15 3 2 2 0

    Good set : 15 3 7 0 0

    Good set : 15 4 4 1 0

    Good set : 15 5 1 0 1

    Good set : 15 5 1 2 0

    Good set : 15 5 6 0 0

    Good set : 15 6 3 1 0

    Good set : 15 7 0 0 1

    Good set : 15 7 0 2 0

    Good set : 15 7 5 0 0

    Good set : 15 8 2 1 0

    Good set : 15 9 4 0 0

    Good set : 15 10 1 1 0

    Good set : 15 11 3 0 0

    Good set : 15 12 0 1 0

    Good set : 15 13 2 0 0

    Good set : 15 15 1 0 0

    Good set : 15 17 0 0 0

    Good set : 20 0 3 0 1

    Good set : 20 0 3 2 0

    Good set : 20 0 8 0 0

    Good set : 20 1 0 1 1

    Good set : 20 1 0 3 0

    Good set : 20 1 5 1 0

    Good set : 20 2 2 0 1

    Good set : 20 2 2 2 0

    Good set : 20 2 7 0 0

    Good set : 20 3 4 1 0

    Good set : 20 4 1 0 1

    Good set : 20 4 1 2 0

    Good set : 20 4 6 0 0

    Good set : 20 5 3 1 0

    Good set : 20 6 0 0 1

    Good set : 20 6 0 2 0

    Good set : 20 6 5 0 0

    Good set : 20 7 2 1 0

    Good set : 20 8 4 0 0

    Good set : 20 9 1 1 0

    Good set : 20 10 3 0 0

    Good set : 20 11 0 1 0

    Good set : 20 12 2 0 0

    Good set : 20 14 1 0 0

    Good set : 20 16 0 0 0

    Good set : 25 0 0 1 1

    Good set : 25 0 0 3 0

    Good set : 25 0 5 1 0

    Good set : 25 1 2 0 1

    Good set : 25 1 2 2 0

    Good set : 25 1 7 0 0

    Good set : 25 2 4 1 0

    Good set : 25 3 1 0 1

    Good set : 25 3 1 2 0

    Good set : 25 3 6 0 0

    Good set : 25 4 3 1 0

    Good set : 25 5 0 0 1

    Good set : 25 5 0 2 0

    Good set : 25 5 5 0 0

    Good set : 25 6 2 1 0

    Good set : 25 7 4 0 0

    Good set : 25 8 1 1 0

    Good set : 25 9 3 0 0

    Good set : 25 10 0 1 0

    Good set : 25 11 2 0 0

    Good set : 25 13 1 0 0

    Good set : 25 15 0 0 0

    Good set : 30 0 2 0 1

    Good set : 30 0 2 2 0

    Good set : 30 0 7 0 0

    Good set : 30 1 4 1 0

    Good set : 30 2 1 0 1

    Good set : 30 2 1 2 0

    Good set : 30 2 6 0 0

    Good set : 30 3 3 1 0

    Good set : 30 4 0 0 1

    Good set : 30 4 0 2 0

    Good set : 30 4 5 0 0

    Good set : 30 5 2 1 0

    Good set : 30 6 4 0 0

    Good set : 30 7 1 1 0

    Good set : 30 8 3 0 0

    Good set : 30 9 0 1 0

    Good set : 30 10 2 0 0

    Good set : 30 12 1 0 0

    Good set : 30 14 0 0 0

    Good set : 35 0 4 1 0

    Good set : 35 1 1 0 1

    Good set : 35 1 1 2 0

    Good set : 35 1 6 0 0

    Good set : 35 2 3 1 0

    Good set : 35 3 0 0 1

    Good set : 35 3 0 2 0

    Good set : 35 3 5 0 0

    Good set : 35 4 2 1 0

    Good set : 35 5 4 0 0

    Good set : 35 6 1 1 0

    Good set : 35 7 3 0 0

    Good set : 35 8 0 1 0

    Good set : 35 9 2 0 0

    Good set : 35 11 1 0 0

    Good set : 35 13 0 0 0

    Good set : 40 0 1 0 1

    Good set : 40 0 1 2 0

    Good set : 40 0 6 0 0

    Good set : 40 1 3 1 0

    Good set : 40 2 0 0 1

    Good set : 40 2 0 2 0

    Good set : 40 2 5 0 0

    Good set : 40 3 2 1 0

    Good set : 40 4 4 0 0

    Good set : 40 5 1 1 0

    Good set : 40 6 3 0 0

    Good set : 40 7 0 1 0

    Good set : 40 8 2 0 0

    Good set : 40 10 1 0 0

    Good set : 40 12 0 0 0

    Good set : 45 0 3 1 0

    Good set : 45 1 0 0 1

    Good set : 45 1 0 2 0

    Good set : 45 1 5 0 0

    Good set : 45 2 2 1 0

    Good set : 45 3 4 0 0

    Good set : 45 4 1 1 0

    Good set : 45 5 3 0 0

    Good set : 45 6 0 1 0

    Good set : 45 7 2 0 0

    Good set : 45 9 1 0 0

    Good set : 45 11 0 0 0

    Good set : 50 0 0 0 1

    Good set : 50 0 0 2 0

    Good set : 50 0 5 0 0

    Good set : 50 1 2 1 0

    Good set : 50 2 4 0 0

    Good set : 50 3 1 1 0

    Good set : 50 4 3 0 0

    Good set : 50 5 0 1 0

    Good set : 50 6 2 0 0

    Good set : 50 8 1 0 0

    Good set : 50 10 0 0 0

    Good set : 55 0 2 1 0

    Good set : 55 1 4 0 0

    Good set : 55 2 1 1 0

    Good set : 55 3 3 0 0

    Good set : 55 4 0 1 0

    Good set : 55 5 2 0 0

    Good set : 55 7 1 0 0

    Good set : 55 9 0 0 0

    Good set : 60 0 4 0 0

    Good set : 60 1 1 1 0

    Good set : 60 2 3 0 0

    Good set : 60 3 0 1 0

    Good set : 60 4 2 0 0

    Good set : 60 6 1 0 0

    Good set : 60 8 0 0 0

    Good set : 65 0 1 1 0

    Good set : 65 1 3 0 0

    Good set : 65 2 0 1 0

    Good set : 65 3 2 0 0

    Good set : 65 5 1 0 0

    Good set : 65 7 0 0 0

    Good set : 70 0 3 0 0

    Good set : 70 1 0 1 0

    Good set : 70 2 2 0 0

    Good set : 70 4 1 0 0

    Good set : 70 6 0 0 0

    Good set : 75 0 0 1 0

    Good set : 75 1 2 0 0

    Good set : 75 3 1 0 0

    Good set : 75 5 0 0 0

    Good set : 80 0 2 0 0

    Good set : 80 2 1 0 0

    Good set : 80 4 0 0 0

    Good set : 85 1 1 0 0

    Good set : 85 3 0 0 0

    Good set : 90 0 1 0 0

    Good set : 90 2 0 0 0

    Good set : 95 1 0 0 0

    Good set : 100 0 0 0 0

    Found 292 solutions.