C#实现24点快速计算【源码/实例下载】
作者:admin 时间:2023-4-13 9:54:41 浏览:在抖音常常看到有人玩24点快速计算,这是一个脑力锻炼智力开发的游戏,就是从扑克牌随机拿出4张,然后快速计算出结果等于24,注意是每张牌只能算一次。
本文提供C#实现24点快速计算的源码。
实例介绍
在输入框里输入1到10的4个数,然后点击“计算24点”按钮,瞬间结果就出现在下方的“结果”方框里。
当无解时,“结果”方框里提示“没有找到合适的方法”。
C#源码
#region 24点算法
/*
* Count24(3,3,7,7);
* 穷举法
*
*/
private string[] countMethod = new string[] { "+", "-", "*", "/" };
private int[] countNum;
private int[] countNumBak;
public string Count24(int a, int b, int c, int d)
{
countNumBak = new int[4] { a, b, c, d };
countNum = new int[4];
string result = "没有找到合适的方法";
bool isTrue;
//把 abcd 四个数字随机付给数组 countNum
for (int i = 0; i < 4; i++)
{
countNum[0] = countNumBak[i];
for (int j = 0; j < 4; j++)
{
if (j == i)
continue;
countNum[1] = countNumBak[j];
for (int k = 0; k < 4; k++)
{
if (k == j || k == i)
continue;
countNum[2] = countNumBak[k];
countNum[3] = countNumBak[1 + 2 + 3 - i - j - k];
result = countMain24(countNum, out isTrue);
if (!isTrue)
{
result = countMain(countNum, out isTrue);
}
if (isTrue)
return result;
else
result = "没有找到合适的方法";
}
}
}
return result;
}
/// <summary>
/// 组合计算,(第一个数字(方法)第二个数字) (方法) (第三个数字)(方法)(第四个数字)
/// </summary>
/// <param name="countNum"></param>
/// <param name="isTrue"></param>
/// <returns></returns>
private string countMain(int[] countNum, out bool isTrue)
{
float a, b, c;
string result = string.Empty;
isTrue = false;
foreach (string method in countMethod)
{
a = eval(method, (float)countNum[0], (float)countNum[1]);
foreach (string m in countMethod)
{
b = eval(method, (float)countNum[2], (float)countNum[3]);
foreach (string n in countMethod)
{
c = eval(n, a, b);
if (Math.Round(c, 4) == 24)
{
result = "(" + countNum[0].ToString() + method + countNum[1].ToString() + ")";
result += n + "(" + countNum[2].ToString() + m + countNum[3].ToString() + ")";
isTrue = true;
goto TODO;
}
}
}
}
TODO:
return result;
}
/// <summary>
/// 顺序计算,第一个数字(方法)第二个数字(方法)第三个数字(方法)第四个数字
/// </summary>
/// <param name="countNum"></param>
/// <param name="isTrue"></param>
/// <returns></returns>
private string countMain24(int[] countNum, out bool isTrue)
{
string result = "";
float countValue = 0;
float countValueBak = 0;
isTrue = false;
float upValueA, upValueBakA;
float upValueB, upValueBakB;
// a (方法) b (方法) c (方法) d
for (int i = 0; i < 4; i++)
{ //不必计算 b/a 的情况,数组重排列中会计算到此种情况
if (countMethod[i] == "/" && countNum[1] == 0)
countValue = (float)countNum[0];
else
countValue = eval(countMethod[i], (float)countNum[0], (float)countNum[1]);
upValueA = countValue;
upValueBakA = countValue;
for (int j = 0; j < 4; j++)
{
//第一种情况 (a和b的结果) (方法) c
if (countMethod[j] == "/" && countNum[2] == 0)
{ }
else
{
countValue = eval(countMethod[j], upValueA, (float)countNum[2]);
}
//第二种情况 c (方法) (a和b的结果)
if (countMethod[j] == "/" && upValueBakA == 0)
{
countValueBak = upValueBakA;
}
else
{
countValueBak = eval(countMethod[j], (float)countNum[2], upValueBakA);
}
upValueB = countValue;
upValueBakB = countValueBak;
for (int k = 0; k < 4; k++)
{
//第一种情况 d (方法) (a,b,c的结果1)
if (countMethod[k] == "/" && upValueB == 0)
{ }
else
{
countValue = eval(countMethod[k], (float)countNum[3], upValueB);
if (Math.Round(countValue, 4) == 24)
{//如果已经得到24点,则结束本程序
result = countNum[3].ToString() + countMethod[k] + "((" + countNum[0].ToString() + countMethod[i] + countNum[1].ToString() + ")";
result += countMethod[j] + countNum[2].ToString() + ")";
result += " = 24";
isTrue = true;
return result;
}
}
//第二种情况 (a,b,c的结果1) (方法) d
if (countMethod[k] == "/" && countNum[3] == 0)
{ }
else
{
countValue = eval(countMethod[k], upValueB, (float)countNum[3]);
if (Math.Round(countValue, 4) == 24)
{//如果已经得到24点,则结束本程序
result = "((" + countNum[0].ToString() + countMethod[i] + countNum[1].ToString() + ")";
result += countMethod[j] + countNum[2].ToString() + ")";
result += countMethod[k] + countNum[3].ToString() + " = 24";
isTrue = true;
return result;
}
}
//第三种情况 d (方法) (a,b,c的结果2)
if (countMethod[k] == "/" && upValueBakB == 0)
{ }
else
{
countValueBak = eval(countMethod[k], (float)countNum[3], upValueBakB);
if (Math.Round(countValueBak, 4) == 24)
{//如果已经得到24点,则结束本程序
result = countNum[3].ToString() + countMethod[k] + "(" + countNum[2].ToString() + countMethod[j] + "(" + countNum[0].ToString() + countMethod[i] + countNum[1].ToString() + "))";
result += " = 24";
isTrue = true;
return result;
}
}
//第四种情况 (a,b,c的结果2) (方法) d
if (countMethod[k] == "/" && countNum[3] == 0)
{ }
else
{
countValueBak = eval(countMethod[k], upValueBakB, (float)countNum[3]);
if (Math.Round(countValueBak, 4) == 24)
{//如果已经得到24点,则结束本程序
result = "(" + countNum[2].ToString() + countMethod[j] + "(" + countNum[0].ToString() + countMethod[i] + countNum[1].ToString() + "))";
result += countMethod[k] + countNum[3].ToString();
result += " = 24";
isTrue = true;
return result;
}
}
}
}
}
return "";
}
private float eval(string method, float a, float b)
{
switch (method)
{
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
if (b == 0)
{
return a;
}
else
{
return a / b;
}
default:
return 0;
}
}
#endregion
实例下载
本实例使用.NET4.0创建,运行本实例你需要在本机安装.NET4.0框架。
相关文章
x
- 站长推荐