帮讲解一下24点的算法!
给出4个1-9之间的自然数,其中每个数字只能使用一次;任意使用 + - * / ( ) ,构造出一个表达式,使得最终结果为24,这就是常见的算24点的游戏。 --------------------编程问答-------------------- 也很希望得到回复!等待大侠!!!!!!!!!!!!! --------------------编程问答-------------------- 友情。帮顶 --------------------编程问答-------------------- http://download.csdn.net/source/356094算法和程序下载
--------------------编程问答-------------------- 思路是先进行四个数的位置的组合,下面是可能的组合:
arr[0],arr[1],arr[2],arr[3]
arr[0],arr[1],arr[3],arr[2]
arr[0],arr[3],arr[2],arr[1]
arr[0],arr[3],arr[1],arr[2]
arr[0],arr[2],arr[3],arr[1]
arr[0],arr[2],arr[1],arr[3]
arr[1],arr[0],arr[2],arr[3]
arr[1],arr[0],arr[3],arr[2]
arr[1],arr[3],arr[2],arr[0]
arr[1],arr[3],arr[0],arr[2]
arr[1],arr[2],arr[3],arr[0]
arr[1],arr[2],arr[0],arr[3]
arr[2],arr[1],arr[0],arr[3]
arr[2],arr[1],arr[3],arr[0]
arr[2],arr[3],arr[0],arr[1]
arr[2],arr[3],arr[1],arr[0]
arr[2],arr[0],arr[3],arr[1]
arr[2],arr[0],arr[1],arr[3]
arr[3],arr[1],arr[2],arr[0]
arr[3],arr[1],arr[0],arr[2]
arr[3],arr[0],arr[2],arr[1]
arr[3],arr[0],arr[1],arr[2]
arr[3],arr[2],arr[0],arr[1]
arr[3],arr[2],arr[1],arr[0]
然后针对上面的每一种组合,比如:arr[0],arr[1],arr[2],arr[3]
1)计算arr[0]和arr[1]的6种运算结果k1:a+b,a-b,a*b,a/b, b-a, b/a
2)计算k1和arr[2]的6种运算结果k2
3)计算k2和arr[3]的6种运算结果k3
如果(k3*10)取整=240 (乘10是为了防止24.01和23.99的情况)
就输出 --------------------编程问答-------------------- 关注+学习!!
帮你顶!!
--------------------编程问答-------------------- --------------------编程问答-------------------- 前几天刚有人发过啊 --------------------编程问答-------------------- 参考如下代码:
private const double Precision = 1E-6; // 精度--------------------编程问答--------------------
private bool fSearchExpression(double[] ANumbers, string[] AExpressions,
int ALevel, int ADest, List<string> AResults)
{
bool Result = false;
if ((ALevel <= 1) && (Math.Abs(ANumbers[0] - ADest) <= Precision))
{
AResults.Add(AExpressions[0]);
return true;
}
for (int i = 0; i < ALevel; i++)
for (int j = i + 1; j < ALevel; j++)
{
double A = ANumbers[i];
double B = ANumbers[j];
ANumbers[j] = ANumbers[ALevel - 1];
string vExpA = AExpressions[i];
string vExpB = AExpressions[j];
AExpressions[j] = AExpressions[ALevel - 1];
AExpressions[i] = '(' + vExpA + '+' + vExpB + ')';
ANumbers[i] = A + B;
if (fSearchExpression(ANumbers, AExpressions,
ALevel - 1, ADest, AResults)) Result = true;
AExpressions[i] = '(' + vExpA + '-' + vExpB + ')';
ANumbers[i] = A - B;
if (fSearchExpression(ANumbers, AExpressions,
ALevel - 1, ADest, AResults)) Result = true;
AExpressions[i] = '(' + vExpB + '-' + vExpA + ')';
ANumbers[i] = B - A;
if (fSearchExpression(ANumbers, AExpressions,
ALevel - 1, ADest, AResults)) Result = true;
AExpressions[i] = '(' + vExpA + '*' + vExpB + ')';
ANumbers[i] = A * B;
if (fSearchExpression(ANumbers, AExpressions,
ALevel - 1, ADest, AResults)) Result = true;
if (B != 0)
{
AExpressions[i] = '(' + vExpA + '/' + vExpB + ')';
ANumbers[i] = A / B;
if (fSearchExpression(ANumbers, AExpressions,
ALevel - 1, ADest, AResults)) Result = true;
}
if (A != 0)
{
AExpressions[i] = '(' + vExpB + '/' + vExpA + ')';
ANumbers[i] = B / A;
if (fSearchExpression(ANumbers, AExpressions,
ALevel - 1, ADest, AResults)) Result = true;
}
ANumbers[i] = A;
ANumbers[j] = B;
AExpressions[i] = vExpA;
AExpressions[j] = vExpB;
}
return Result;
}
private bool SearchExpression(List<string> AResults, int ADest, params int[] ANumbers)
{
double[] vNumbers = new double[ANumbers.Length];
string[] vExpressions = new string[ANumbers.Length];
for (int i = 0; i < ANumbers.Length; i++)
{
vNumbers[i] = ANumbers[i];
vExpressions[i] = ANumbers[i].ToString();
}
return fSearchExpression(vNumbers, vExpressions, ANumbers.Length, ADest, AResults);
}
private void button1_Click(object sender, EventArgs e)
{
List<string> vExpressions = new List<string>();
SearchExpression(vExpressions, 24, 3, 3, 7, 7);
foreach (string vExpression in vExpressions)
textBox1.AppendText(vExpression + "\r\n");
}
这个思路清晰 --------------------编程问答-------------------- zswang 的写法,真头痛呀! --------------------编程问答-------------------- 全排列···
DFS也可以的···看你喜欢怎么搞 --------------------编程问答--------------------
这是最暴力的解法……看不明白zwang的算法
补充:.NET技术 , C#