[thechat] Programming Problem
Joe Crawford
jcrawford at avencom.com
Thu Nov 8 17:23:26 CST 2001
Beau Hartshorne wrote:
> My girlfriend introduced me to a card game called "24". In the game,
> aces are valued at 1 and royalty is valued at 10. Four cards are laid
> out on the table, and the first person to figure out a way to somehow
> get the cards to add/multiply/subtract/divide evenly to 24 wins the
> round. Each card can be used in the calculation only once.
>
> So if these four cards were laid out: 7 3 1 3
> One possible solution is: 7*3+3*1
>
> For these cards: 2 10 1 2
> One possible solution is: (1+2)*(10-2)
>
> My question is how would you go about writing a computer program to find
> every possible solution for every possible combination of cards?
>
> Ideas?
In php - something like --- (don't laugh, ( can tell you that this is
absolutely the least efficient way to code this. And more, I can't get
it to do the math! But the concept is arrays of the possible, and
iterate through them all.
This gets to:
3*3*Jack-10 = 3*3*10-10 =
3*3*Jack-Jack = 3*3*10-10 =
3*3*Jack-Queen = 3*3*10-10 =
3*3*Jack-King = 3*3*10-10 =
Fatal error: Maximum execution time of 60 seconds exceeded in
/<deleted>p on line 28
Somebody should tell rudy about this thread - this seems up his alley. I
bet he could do it in SQL! I'll cc him. :-)
- Joe <http://artlung.com/>
<pre><?
$card_values = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10,
10);
$card_names =
array("Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King");
$operators = array("+","-","/","*");
for($card1=0;$card1<sizeof($card_names);$card1++)
{
for($op1=0;$op1<sizeof($operators);$op1++)
{
for($card2=0;$card2<sizeof($card_names);$card2++)
{
for($op2=0;$op2<sizeof($operators);$op2++)
{
for($card3=0;$card3<sizeof($card_names);$card3++)
{
for($op3=0;$op3<sizeof($operators);$op3++)
{
for($card4=0;$card4<sizeof($card_names);$card4++)
{
echo $card_names[$card1];
echo $operators[$op1];
echo $card_names[$card2];
echo $operators[$op2];
echo $card_names[$card3];
echo $operators[$op3];
echo $card_names[$card4];
$card_operation =
$card_values[$card1]
.$operators[$op1]
.$card_values[$card2]
.$operators[$op2]
.$card_values[$card3]
.$operators[$op3]
.$card_values[$card4];
echo " = $card_operation = ";
echo "\n";
}
}
}
}
}
}
}
?></pre>
--
................... Joe Crawford \\ Web Design & Development
..... mailto:jcrawford at avencom.com \\ http://www.avencom.com
.... San Diego \\ CA \\ USA \\ AVENCOM: Set Your Sites Higher
More information about the thechat
mailing list