问题描述:
24点游戏是经典的纸牌益智游戏。
常见游戏规则:
从扑克中每次取出4张牌。使用加减乘除,第一个能得出24者为赢。(其中,J代表11,Q代表12,K代表13,A代表1),按照要求编程解决24点游戏。
基本要求: 随机生成4个代表扑克牌牌面的数字字母,程序自动列出所有可能算出24的表达式,用擅长的语言(C/C++/Java或其他均可)实现程序解决问题。
1.算法分析
随机生成四个数,采用穷举的方法,对每个数字和操作符进行组合,从而找到所有的情况,输出所有的表达式;若得不出24点则输出无法得出结果。
2.概设计
代码区
import java.util.Random;
public class twentyfour {
private final static char[] op = { '+', '-', '*', '/' }; //存运算符
private static float cal(float x, float y, int op) {
float result = 0;
switch (op) {
case 0:
result = x + y;
break;
case 1:
result = x - y;
break;
case 2:
result = x * y;
break;
case 3:
result = x / y;
break;
}
return result;
} //cal函数计算x op y
private static float cal1(float a, float b, float c, float d, int op1, int op2, int op3)
{
float r1, r2, r3;
r1 = cal(a, b, op1);
r2 = cal(c, d, op3);
r3 = cal(r1, r2, op2);
return r3;
} //cal1计算情况(a op1 b) op2 (c op3 d)
private static float cal2(float a, float b, float c, float d, int op1, int op2, int op3)
{
float r1, r2, r3;
r1 = cal(a, b, op1);
r2 = cal(r1, c, op2);
r3 = cal(r2, d, op3);
return r3;
} //cal2计算情况((a op1 b)op2 c) op3 d
private static float cal3(float a, float b, float c, float d, int op1, int op2, int op3)
{
float r1, r2, r3;
r1 = cal(b, c, op2);
r2 = cal(r1, d, op3);
r3 = cal(a, r2, op1);
return r3;
} //cal3计算情况a op1 ((b op2 c) op3 d)
private static float cal4(float a, float b, float c, float d, int op1, int op2, int op3)
{
float r1, r2, r3;
r1 = cal(c, d, op3);
r2 = cal(b, r1, op2);
r3 = cal(a, r2, op1);
return r3;
} //cal4计算情况a op1 (b op2 (c op3 d))
private static float cal5(float a, float b, float c, float d, int op1, int op2, int op3)
{
float r1, r2, r3;
r1 = cal(b, c, op2);
r2 = cal(a, r1, op1);
r3 = cal(r2, d, op3);
return r3;
} //cal5计算情况(a op1 (b op2 c)) op3 d
static boolean rand(int a,int b,int c, int d)
{
boolean flag=false; //运算成功返回true
for(int op1 = 0;op1<=3;op1++)
{
for(int op2 = 0;op2<=3;op2++)
{
for(int op3 = 0;op3<=3;op3++)
{
if(cal1(a, b, c, d, op1, op2, op3)==24){
System.out.println("("+a+op[op1]+b +")"+ op[op2] +"( "+c+op[op3]+d+")"+" = 24");
flag=true;
} // 调用表达式1的方法 (a op1 b)op2 (c op3 d)
if(cal2(a, b, c, d, op1, op2, op3)==24){
System.out.println("(("+a+op[op1]+b +")"+ op[op2]+ c +")"+op[op3]+d+" = 24");
flag=true;
} // 调用表达式2的方法 ((a op1 b)op2 c) op3 d
if(cal3(a, b, c, d, op1, op2, op3)==24){
System.out.println("( "+a+op[op1]+"(("+b+ op[op2]+ c +")"+op[op3]+d +")) = 24");
flag=true;
} // 调用表达式3的方法 a op1 ((b op2 c) op3 d)
if(cal4(a, b, c, d, op1, op2, op3)==24){
System.out.println("( "+a+op[op1]+"("+b+ op[op2]+ "(" +c +op[op3]+d +"))) = 24");
flag=true;
} // 调用表达式4的方法 a op1 (b op2 (c op3 d))
if(cal5(a, b, c, d, op1, op2, op3)==24){
System.out.println("("+a+op[op1]+"("+b+ op[op2]+ c +"))"+op[op3]+d +" = 24");
flag=true;
} // 调用表达式5的方法 (a op1 (b op2 c)) op3 d
}
}
}
return flag;
}
public static void main(String[] args) {
Random rand = new Random();
int a = rand.nextInt(13) + 1;
int b = rand.nextInt(13) + 1;
int c = rand.nextInt(13) + 1;
int d = rand.nextInt(13) + 1;
//int a=11,b=1,c=4,d=3;
System.out.println("随机生成4个1-13的数:"+a+" "+b+" "+c+" "+d);
if(!rand(a,b,c,d)) {
System.out.println("无法得到24点。");
}
}
}