您的位置:首页 > 编程语言 > Java开发

Java扑克牌速算24的方法

2020-04-06 12:06 1221 查看

已知一副扑克牌有54张,去除大王和小王,剩余52张。在其中随机抽取4张牌,利用加减乘除进行计算得到24. 从A到10,他们的值分别为1到10. 从J到K,他们对应的值是减去10以后的值。编写程序生成一副扑克牌,随机抽取4张,进行计算是否能得到24. 如果可以,列出可能的计算表达式,可能有多种计算形式。

主要思想就是先生成除去大小王的52张扑克牌,然后从中随机抽取四张。接着用排列组合的方式找到能计算得到24的表达式。

package Poker;
import java.util.Random;
import java.util.Stack;
public class Poker {
static int count = 0;
static int count_2 = 0;
public static Stack<Integer> stack = new Stack<Integer>();
public static Stack<Integer> stack_temp = new Stack<Integer>();
public static Stack<String> stack_collection = new Stack<String>();
public static void main(String[] args) {
System.out.println("Poker World");
// 生成扑克牌
int poker[][] = new int[4][13];
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 13; j++) {
if(j + 1 < 11) {
poker[i][j] = j+1;
}
else {
poker[i][j] = j-9;
}
}
}
Random r = new Random();
int randomPoker[] = new int[4];
for(int i = 0; i < 4; i++) {
randomPoker[i] = poker[r.nextInt(3)][r.nextInt(12)];
stack_temp.add(randomPoker[i]);
}
System.out.print("抽到的扑克牌:");
for(int element: randomPoker) {
System.out.printf("%d ", element);
}
System.out.print("\n");
order(randomPoker, 4, 0);
//System.out.printf("%d\n",count_2);
if(count != 0) {
System.out.printf("共有%d种计算方法", count);
}
else {
System.out.print("无法计算得到24");
}
}
private static void order(int[] poker, int targ, int cur) {
// TODO Auto-generated method stub
if(cur == targ) {
String str = stack.toString();
if(!stack_collection.contains(str)) {
stack_collection.add(str);
count_2 += 1;
int new_poker[] = new int[4];
for(int i = 0; i < 4; i++) {
new_poker[i] = stack.pop();
}
for(int i = 3; i >= 0; i--) {
stack.push(new_poker[i]);
}
do24(new_poker);
}
return;
}
for(int i = 0; i < poker.length; i++) {
if(stack_temp.contains(poker[i])) {
stack.add(poker[i]);
for(int j = 0; j < stack_temp.size(); j++) {
if(stack_temp.get(j) == poker[i]) {
stack_temp.remove(j);
break;
}
}
order(poker, targ, cur+1);
stack.pop();
stack_temp.add(poker[i]);
}
}
}

public static void do24(int[] poker){
// 四个数字中间有三个运算符,因此用三重循环遍历。其中0表示+,1表示-,2表示x,3表示÷
int test[] = {1,2,3,4};
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
for(int k=0;k<4;k++){
operate(poker,i,j,k);
//operate(test,i,j,k);
}
}
}

}
/*
* 分类讨论
*/
public static void operate(int[] poker, int op1, int op2, int op3) {
int num1 = poker[0];
int num2 = poker[1];
int num3 = poker[2];
int num4 = poker[3];
if((cal(cal(cal(num1,num2,op1),num3,op2),num4,op3) == 24)) {
if((((op2 == 0) || (op2 == 1))&&((op3 == 0) || (op3 == 1)))||((op2 != 0) && (op2 != 1))) {
System.out.println("("+num1+symbol(op1)+num2+")"+symbol(op2)+num3+symbol(op3)+num4);
count++;
}
}
else if(cal(cal(num1,cal(num2,num3,op2),op1),num4,op3) == 24) {
if((((op1 == 0) || (op1 == 1))&&((op3 == 0) || (op3 == 1)))||((op1 != 0) && (op1 != 1))) {
System.out.println(num1+symbol(op1)+"("+num2+symbol(op2)+num3+")"+symbol(op3)+num4);
count++;
}
else {
System.out.println("["+num1+symbol(op1)+"("+num2+symbol(op2)+num3+")]"+symbol(op3)+num4);
count++;
}
}
else if(cal(cal(num1,num2,op1),cal(num3,num4,op3),op2) == 24) {
if((((op1 == 0) || (op1 == 1))&&((op2 == 0) || (op2 == 1)))||((op1 != 0) && (op1 != 1))) {
System.out.println(num1+symbol(op1)+num2+symbol(op2)+"("+num3+symbol(op3)+num4+")");
count++;
}
else {
System.out.println("("+num1+symbol(op1)+num2+")"+symbol(op2)+"("+num3+symbol(op3)+num4+")");
count++;
}
}
else if(cal(cal(cal(num1,num2,op1),num3,op2),num4,op3) == 24) {
if((((op1 == 0) || (op1 == 1))&&((op2 == 0) || (op2 == 1)))||((op1 != 0) && (op1 != 1))) {
System.out.println("("+num1+symbol(op1)+num2+symbol(op2)+num3+")"+symbol(op3)+num4);
count++;
}
else {
System.out.println("[("+num1+symbol(op1)+num2+")"+symbol(op2)+num3+"]"+symbol(op3)+num4);
count++;
}
}
else if(cal(num1,cal(cal(num2,num3,op2),num4,op3),op1) == 24) {
if((((op2 == 0) || (op2 == 1))&&((op3 == 0) || (op3 == 1)))||((op2 != 0) && (op2 != 1))) {
System.out.println(num1+symbol(op1)+"("+num2+symbol(op2)+num3+symbol(op3)+num4+")");
count++;
}
else {
System.out.println(num1+symbol(op1)+"[("+num2+symbol(op2)+num3+")"+symbol(op3)+num4+"]");
count++;
}
}
else if(cal(num1,cal(num2,cal(num3,num4,op3),op2),op1) == 24) {
System.out.println(num1+symbol(op1)+"["+num2+symbol(op2)+"("+num3+symbol(op3)+num4+")]");
count++;
}
}
/*
* 按顺序计算
*/
public static double cal(double num1,double num2,int num){
double sum=0.0;
switch(num) {
case 0:
sum = num1 + num2;
break;
case 1:
sum = num1 - num2;
break;
case 2:
sum = num1 * num2;
break;
case 3:
if(num1 % num2 == 0) {
sum = num1 / num2;
}
else {
sum = 999;
}
break;
default:
break;
}
return sum;
}
/*
* 将代表计算符号的数字转换成字符存入String数组并返回
*/
public static String symbol(int symbolNum){
String symbol = "";
switch (symbolNum) {
case 0:
symbol="+";
break;
case 1:
symbol="-";
break;
case 2:
symbol="x";
break;
case 3:
symbol="÷";
break;
default:
break;
}
return symbol;
}
}

以上就是本文的全部内容,希望对大家的学习有所帮助

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 扑克牌 速算