Sender: wfaye (Down with NATO), Board: GreatTurn
Title: A program to make 24
Source: BBS Shuimu Tsinghua Station (Wed Feb 7 10:48:30 2001)
Seeing everyone persistently trying to solve the 24-point problem, I might as well post a program.
It can solve any 24-point problem within 1 second. Of course, if you want to calculate 25, 26...
that is also possible. I hope this can serve as the definitive end to the 24 problem.
#include "stdafx.h"
//
//Principle: arrange 4 numbers and 3 operators into a sequence in "Polish notation",
// then calculate whether the value of that sequence is the target we want.
// We can traverse the possible compositions of this sequence
// to determine whether a solution exists.
//According to my calculation, if we only use numbers from 1 to 10 to make 24, there are
// 715 different problems in total, of which 566 have solutions. If it is 1 to 13, then there are
// 1820 different problems, of which 1362 have solutions
//
int total = 0; //number of solutions
int sp; //current expression stack pointer
int s; //expression stack
void Evaluate(int& fz, int& fm)
//calculate the value of the expression; fz, fm are the numerator and denominator of the result
{
int op, l, m, opn;
op = s; //take the top stack element
for (l = 0; l 0) //is a number
{
opn = m;
opn = 1;
}
else //is an operator
Evaluate(opn, opn);
}
//calculate according to the operator
//opn/opn is the first operand,
//opn/opn is the second operand,
switch (op)
{
case -4: //multiplication
fz = opn * opn;
fm = opn * opn;
break;
case -3: //addition
fz = opn * opn + opn * opn;
fm = opn * opn;
break;
case -2: //subtraction
fz = opn * opn - opn * opn;
fm = opn * opn;
break;
case -1: //division
fz = opn * opn;
fm = opn * opn;
break;
}
}
void Display(CString& m)
//convert the expression into a string
{
int i;
CString n;
m = "";
for (i = 0; i < 7; i++)
{
switch (s)
{
case -4: m += " *"; break;
case -3: m += " +"; break;
case -2: m += " -"; break;
case -1: m += " /"; break;
default: n.Format("%3d", s); m += n; break;
}
}
}
void Compute(int target, int a, int b, int c, int d, CStringArray& ma)
// target - result to compute (usually 24)
// a, b, c, d - the 4 numbers used in the calculation
// ma - string form of the solutions
{
int l1, l2, l3, op1, op2, op3;
int l, fz, fm, num;
CString m;
//record the arrangement of the middle four elements in the expression
// where loc, loc indicate the positions of the second and third operators
// loc, loc indicate the positions of the first and second operands
int loc = {{1, 2, 3, 4}, {1, 3, 2, 4}, {1, 4, 2, 3},
{2, 3, 1, 4}, {2, 4, 1, 3}};
//num records one permutation of a, b, c, d
for (l1 = 0; l1 < 4; l1++)
{
num = a;
for (l2 = 0; l2 < 4; l2++)
{
if (l2 == l1) continue;
num = b;
for (l3 = 0; l3 < 4; l3++)
{
if (l3 == l1 || l3 == l2) continue;
num = c;
num = d;
//the last two of the expression must be numbers
s = num;
s = num;
for (l = 0; l < 5; l++)
{
s] = num;
s] = num;
for (op1 = -4; op1 < 0; op1++)
{
//the first one in the expression must be an operator
s = op1;
for (op2 = -4; op2 < 0; op2++)
{
s] = op2;
for (op3 = -4; op3 < 0; op3++)
{
s] = op3;
sp = 0;
Evaluate(fz, fm);
//denominator not 0, divisible, quotient 24 means success
if (fm != 0 && fz % fm == 0 && fz / fm == target)
{
Display(m);
ma.Add(m);
total++;
//adding return here means only finding one solution,
//otherwise it finds all solutions (without removing duplicates)
return;
}
}
}
}
}
}
}
}
}
--
※ Source:·BBS Shuimu Tsinghua Station smth.org·
(This article was copied using the S-Term article copy script)
==================================================
Title: A program to make 24
Source: BBS Shuimu Tsinghua Station (Wed Feb 7 10:48:30 2001)
Seeing everyone persistently trying to solve the 24-point problem, I might as well post a program.
It can solve any 24-point problem within 1 second. Of course, if you want to calculate 25, 26...
that is also possible. I hope this can serve as the definitive end to the 24 problem.
#include "stdafx.h"
//
//Principle: arrange 4 numbers and 3 operators into a sequence in "Polish notation",
// then calculate whether the value of that sequence is the target we want.
// We can traverse the possible compositions of this sequence
// to determine whether a solution exists.
//According to my calculation, if we only use numbers from 1 to 10 to make 24, there are
// 715 different problems in total, of which 566 have solutions. If it is 1 to 13, then there are
// 1820 different problems, of which 1362 have solutions
//
int total = 0; //number of solutions
int sp; //current expression stack pointer
int s; //expression stack
void Evaluate(int& fz, int& fm)
//calculate the value of the expression; fz, fm are the numerator and denominator of the result
{
int op, l, m, opn;
op = s; //take the top stack element
for (l = 0; l 0) //is a number
{
opn = m;
opn = 1;
}
else //is an operator
Evaluate(opn, opn);
}
//calculate according to the operator
//opn/opn is the first operand,
//opn/opn is the second operand,
switch (op)
{
case -4: //multiplication
fz = opn * opn;
fm = opn * opn;
break;
case -3: //addition
fz = opn * opn + opn * opn;
fm = opn * opn;
break;
case -2: //subtraction
fz = opn * opn - opn * opn;
fm = opn * opn;
break;
case -1: //division
fz = opn * opn;
fm = opn * opn;
break;
}
}
void Display(CString& m)
//convert the expression into a string
{
int i;
CString n;
m = "";
for (i = 0; i < 7; i++)
{
switch (s)
{
case -4: m += " *"; break;
case -3: m += " +"; break;
case -2: m += " -"; break;
case -1: m += " /"; break;
default: n.Format("%3d", s); m += n; break;
}
}
}
void Compute(int target, int a, int b, int c, int d, CStringArray& ma)
// target - result to compute (usually 24)
// a, b, c, d - the 4 numbers used in the calculation
// ma - string form of the solutions
{
int l1, l2, l3, op1, op2, op3;
int l, fz, fm, num;
CString m;
//record the arrangement of the middle four elements in the expression
// where loc, loc indicate the positions of the second and third operators
// loc, loc indicate the positions of the first and second operands
int loc = {{1, 2, 3, 4}, {1, 3, 2, 4}, {1, 4, 2, 3},
{2, 3, 1, 4}, {2, 4, 1, 3}};
//num records one permutation of a, b, c, d
for (l1 = 0; l1 < 4; l1++)
{
num = a;
for (l2 = 0; l2 < 4; l2++)
{
if (l2 == l1) continue;
num = b;
for (l3 = 0; l3 < 4; l3++)
{
if (l3 == l1 || l3 == l2) continue;
num = c;
num = d;
//the last two of the expression must be numbers
s = num;
s = num;
for (l = 0; l < 5; l++)
{
s] = num;
s] = num;
for (op1 = -4; op1 < 0; op1++)
{
//the first one in the expression must be an operator
s = op1;
for (op2 = -4; op2 < 0; op2++)
{
s] = op2;
for (op3 = -4; op3 < 0; op3++)
{
s] = op3;
sp = 0;
Evaluate(fz, fm);
//denominator not 0, divisible, quotient 24 means success
if (fm != 0 && fz % fm == 0 && fz / fm == target)
{
Display(m);
ma.Add(m);
total++;
//adding return here means only finding one solution,
//otherwise it finds all solutions (without removing duplicates)
return;
}
}
}
}
}
}
}
}
}
--
※ Source:·BBS Shuimu Tsinghua Station smth.org·
(This article was copied using the S-Term article copy script)
==================================================
ko20010214
=================================
大功告成,打个Kiss!
ko20010214@MSN.com
神州优雅Q300C
Intel CeleronM 370处理器 | 256MbDDR内存
40G硬盘 | USB2.0 | IEEE 1394
13.3 ' WXGA 宽屏(16:10) | COMBO光驱
10/100M网卡 | 四合一读卡器
=================================
大功告成,打个Kiss!
ko20010214@MSN.com
神州优雅Q300C
Intel CeleronM 370处理器 | 256MbDDR内存
40G硬盘 | USB2.0 | IEEE 1394
13.3 ' WXGA 宽屏(16:10) | COMBO光驱
10/100M网卡 | 四合一读卡器

*pp2)='+'; break;
;