读书人

怎么拿算24点

发布时间: 2012-02-24 16:30:39 作者: rapoo

如何拿算24点?
很苦恼,感到一筹莫展,4个从1到9的数,四则混合运算。可以用括号,若能得到24,输出。
请高手讲个算法。
问题是有括号
比如(3+3/7)×7=24

[解决办法]
#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)
return result;
else
result = "没有找到合适的方法 ";
}
}
}

return result;
}

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


[解决办法]
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'24点算法
'调用示例 GetNum(6,6,6,6)
Sub GetNum(One,Two,Three,Four)
'24算法,主函数.掉用方法 GetNum(3,3,8,8) .即返回计算24点结果
Dim Varry(4)
Varry(0) = One
Varry(1) = Two
Varry(2) = Three
Varry(3) = Four
For i = 0 to 3
a = Varry(i)
For j = 0 to 3
If j <> i Then
b = Varry(j)
For k = 0 to 3
if k <> j and k <> i Then
c = Varry(k)
l = 1+2+3 - i - j - k
d = Varry(l)

If Count24(a,b,c,d)=True Then Exit Sub
end if
Next
End If
Next
Next
Response.Write( "对不起,找不到合适的方法构成24点 ")
End Sub

Function TrueOrFalse(Method,Str)
If Method= "/ " And Str=0 Then
TrueOrFalse=False
Else
TrueOrFalse=True
End If
End Function

Function GetMathod(str)
Select Case Str
Case 1
GetMathod = "+ "
Case 2
GetMathod = "- "
Case 3
GetMathod = "* "
Case 4
GetMathod = "/ "
End Select
End Function

Function Count24(a,b,c,d)
'2005-9-22,HJ,更新。
Dim abc1,abcd2,abcd3,abcd1,abcd4,abcd5,abc
For i = 1 to 4
If TrueOrFalse(GetMathod(i),b)=False Then i=i+1
ab = Eval(a & GetMathod(i) & b)
For j = 1 to 4
If TrueOrFalse(GetMathod(j),c)=False Then j=j+1


abc = Eval(ab & GetMathod(j) & c)
'***************************************
'漏掉
if ab <> 0 Then abc1 = c / ab
abc2 = c - ab
'***************************************
For k = 1 to 4

If TrueOrFalse(GetMathod(k),d)=False Then k=k+1
abcd = Eval(abc & GetMathod(k) & d)
'****************************************************
'补充各种情况
abcd2 = Eval(abc1 & GetMathod(k) & d)
abcd4 = Eval(abc2 & GetMathod(k) & d)
If abc <> 0 Then abcd1 = d / abc
If abc1 <> 0 and abc1 <> " " Then abcd3 = d / abc1
if abc2> 0 Then abcd5 = d / abc2
'****************************************************

If Trim(abcd)=24 then
Count24 = True
Response.Write( "给出数字: "&a& ", "&b& ", "&c& ", "&d& ", "& "结果: <br> ")
Response.Write(a & GetMathod(i) & b & " = " & ab & " <br> ")
Response.Write(ab & GetMathod(j) & c & " = " &abc & " <br> ")
Response.Write(abc & GetMathod(k) & d & " = "&abcd & " <br> ")
Response.Write( "(( "&a & GetMathod(i) & b & ") "& GetMathod(j) & c & ") " & GetMathod(k) & d & " = "&abcd & " <br> ")
Exit Function
ElseIf Trim(abcd1) = 24 Then
Count24 = True
Response.Write( "给出数字: "&a& ", "&b& ", "&c& ", "&d& ", "& "结果: <br> ")
Response.Write(a & GetMathod(i) & b & " = " & ab & " <br> ")
Response.Write(ab & GetMathod(j) & c & " = " &abc & " <br> ")
Response.Write(d & "/ " &abc& " = "&abcd & " <br> ")
Response.Write(d & "/(( "&a & GetMathod(i) & b & ") "& GetMathod(j) & c & ") = "&abcd1 & " <br> ")
Exit Function
ElseIf Trim(abcd2) = 24 Then
Count24 = True
Response.Write( "给出数字: "&a& ", "&b& ", "&c& ", "&d& ", "& "结果: <br> ")
Response.Write(a & GetMathod(i) & b & " = " & ab & " <br> ")
Response.Write(c & "/ "& ab & "= " & abc1 & " <br> ")
Response.Write(abc1 & GetMathod(k) & d& " = "&abcd & " <br> ")
Response.Write( "( "&c& "/( "&a & GetMathod(i) & b & ")) "& GetMathod(k) & d & " = "&abcd2 & " <br> ")
Exit Function
ElseIf Trim(abcd3) = 24 Then
Count24 = True
Response.Write( "给出数字: "&a& ", "&b& ", "&c& ", "&d& ", "& "结果: <br> ")
Response.Write(a & GetMathod(i) & b & " = " & ab & " <br> ")
Response.Write(c & "/ "& ab & "= " & abc1 & " <br> ")
Response.Write(d & "/ "& abc1 & "= "&abcd3 & " <br> ")
Response.Write(d & "/( "&c& "/( "&a & GetMathod(i) & b & ")) = "&abcd3 & " <br> ")


Exit Function
ElseIf Trim(abcd4) = 24 Then
Count24 = True
Response.Write( "给出数字: "&a& ", "&b& ", "&c& ", "&d& ", "& "结果: <br> ")
Response.Write(a & GetMathod(i) & b & " = " & ab & " <br> ")
Response.Write(c & "- "& ab & "= " & abc2 & " <br> ")
Response.Write(abc2 & GetMathod(k) & d& "= "&abcd4 & " <br> ")
Response.Write( "( "&c& "-( "&a & GetMathod(i) & b & ")) "& GetMathod(k) & d & " = "&abcd4 & " <br> ")
Exit Function
ElseIf cint(abcd5) = 24 Then
Count24 = True
Response.Write( "给出数字: "&a& ", "&b& ", "&c& ", "&d& ", "& "结果: <br> ")
Response.Write(a & GetMathod(i) & b & " = " & ab & " <br> ")
Response.Write(c & "- "& ab & "= " & abc2 & " <br> ")
Response.Write(abc2 & GetMathod(k) & d& "= "&abcd4 & " <br> ")
Response.Write(d& "/( "&c& "-( "&a & GetMathod(i) & b & ")) = "&abcd5 & " <br> ")
Exit Function
end If
Next
Next
Next
Count24 = False
End Function
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

读书人网 >C#

热点推荐