24点游戏的算法,貌似用二叉树。懂的人进来说说。谢谢!

fihuang 2012-03-11 05:02:19
24点游戏的算法,貌似用二叉树。懂的人进来说说。谢谢!

四个数,三个符号。运算出来的结果为24.
...全文
300 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
kosora曹 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

怎么都没有二叉树吗?难道是我记错了?我记得数据结果讲二叉树的时候貌似说到过加减乘除的。
[/Quote]
二叉树的加减乘除是前缀、中缀、后缀表达式。
fihuang 2012-03-13
  • 打赏
  • 举报
回复
怎么都没有二叉树吗?难道是我记错了?我记得数据结果讲二叉树的时候貌似说到过加减乘除的。
fihuang 2012-03-12
  • 打赏
  • 举报
回复
to maco_wang
谢谢 不知道有没有二叉树的算法 我印象里好像有
wanghui0380 2012-03-12
  • 打赏
  • 举报
回复
这个真没有,所谓的二叉树的算法。其实是中缀表达式。实际上个人建议后缀表达式,也就是一般说的逆波兰表达式
linxi729778865 2012-03-12
  • 打赏
  • 举报
回复
24点全解

using System;
using System.Collections.Generic;
using System.Text;

class TwentyFourProblem
{
static void Main()
{
string datas = "";
string operators = @"+_-_*_/_|_\";
string temData = "";
for (int i = 0; i < 4; i++)
datas += Console.ReadLine() + "_";
List<string> dataPermutations = new List<string>();
List<string> operatorsCombinations = new List<string>();
List<string> solutions = new List<string>();
AllPermutation(datas, temData, dataPermutations);
AllCombination(1, 3, operators, "", operatorsCombinations);
foreach (string temDatas in dataPermutations)
foreach (string temOperators in operatorsCombinations)
{
calculate(temDatas, temOperators, "", "", solutions);
}
foreach (string solution in solutions)
Console.Write(solution + "\n");
Console.Write("over");
}


private static void AllPermutation(string datas, string temData, List<string> permutations)
{
string[] data = datas.Split('_');
for (int i = 0; i < data.Length - 1; i++)
{
if (data.Length == 2)
{
string temPermutation = temData + data[i];
permutations.Add(temPermutation);
}
else
{
string tem = temData;
temData += data[i] + '_';
datas = "";
for (int j = 0; j < data.Length - 1; j++)
if (j != i)
datas += data[j] + '_';
AllPermutation(datas, temData, permutations);
temData = tem;
}
}
}

private static void AllCombination(int rankCount, int rank, string datas, string temCombination, List<string> combinations)
{
string[] data = datas.Split('_');
for (int i = 0; i < data.Length; i++)
{
if (rankCount == rank)
combinations.Add(temCombination + data[i]);
else
{
string temStr = temCombination;
temCombination += data[i] + "_";
AllCombination(++rankCount, rank, datas, temCombination, combinations);
rankCount--;
temCombination = temStr;
}
}
}

private static void calculate(string datas, string operators, string lastOperator, string temSolution, List<string> solutions)
{
string[] data = datas.Split('_');
string[] temOperator = operators.Split('_');
if (data.Length > 1)
{
string result = operatorOperation(data[0], data[1], temOperator[0]).ToString();
if ((temOperator[0] == "\\" && lastOperator != "") || (temOperator[0] == "|" && (lastOperator == "-" || lastOperator == "+" || lastOperator == "|")) || ((temOperator[0] == "*" || temOperator[0] == "/") && (lastOperator == "+" || lastOperator == "-" || lastOperator == "|")))
{
temSolution = temSolution.Insert(0, "(");
temSolution += ")";
}
if (data.Length == 4)
temSolution += data[0];
if (temOperator[0] == "|" || temOperator[0] == "\\")
{
string temOp = "";
if (temOperator[0] == "|")
temOp = "-";
else temOp = "/";
temSolution = temSolution.Insert(0, data[1] + temOp);
}
else temSolution += temOperator[0] + data[1];
string newDatas = "";
string newOperators = "";
for (int j = 2; j < data.Length; j++)
newDatas += data[j] + "_";
newDatas = newDatas.Insert(0, result + "_");
for (int j = 1; j < temOperator.Length; j++)
newOperators += temOperator[j] + "_";
if (newOperators == "")
newOperators = "?";
calculate(newDatas.Substring(0, newDatas.Length - 1), newOperators.Substring(0, newOperators.Length - 1), temOperator[0], temSolution, solutions);
}
else if (data.Length == 1)
{
if (Math.Abs(double.Parse(data[0]) - 24) <= 0.000001)
solutions.Add(temSolution);
}
}

private static double operatorOperation(string data1, string data2, string Operator)
{
double result = 0;
double num1 = double.Parse(data1);
double num2 = double.Parse(data2);
switch (Operator)
{
case "+": result = num1 + num2; break;
case "-": result = num1 - num2; break;
case "*": result = num1 * num2; break;
case "/": result = num1 / num2; break;
case "|": result = num2 - num1; break;
case "\\": result = num2 / num1; break;
}
return result;
}
}
叶子 2012-03-11
  • 打赏
  • 举报
回复
叶子 2012-03-11
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;

namespace MyCsStudy
{
/// <summary>
/// 24点算法
/// </summary>
class TwentyFour
{
public static List<string> listOps = new List<string>(); //存加减乘除运算符
static TwentyFour()
{
listOps.Add("+");
listOps.Add("-");
listOps.Add("*");
listOps.Add("/");
}
public static bool Computing(string[] numArr, int numLen, int targetNum)
{
bool flag = false;
for (int i = 0; i < numLen; i++)
{
for (int j = i + 1; j < numLen; j++)
{
string[] strij = new string[] { numArr[i], numArr[j] };
numArr[j] = numArr[numLen - 1];
for (int k = 0; k < listOps.Count; k++)
{
numArr[i] = "(" + strij[0] + listOps[k] + strij[1] + ")";
if (Computing(numArr, numLen - 1, targetNum))
{
flag = true;
return flag;
}
}
numArr[i] = strij[0];
numArr[j] = strij[1];
}
}
System.Data.DataTable dt = new System.Data.DataTable();
string expression = numArr[0];
/*
* 利用datatable的compute方法计算字符串表达式(不知c有没有类似方法)
* 如果不用datatable的compute方法,不知还有没有其他c#函数?
* 期待高手给出更直观的解答
*/
object objNum = dt.Compute(expression, "");
int result;
int.TryParse(objNum.ToString(), out result);

//如果运算结果是预期的数字,而且数组中的数字全部在运算范畴内
if (result == targetNum && numLen == 1)
flag = true;
return flag;
}


110,552

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧