There are 54 cards in a deck of playing cards, and the king and the little king are removed, and the remaining 52 cards are left. 4 cards are randomly drawn, and 24 is calculated using addition, subtraction, multiplication and division. From A to 10, their values are 1 to 10 respectively. From J to K, their corresponding values are the values after subtracting 10. Write a program to generate a deck of playing cards, randomly draw 4 pieces, and calculate whether you can get 24. If possible, list possible calculation expressions, and there may be multiple forms of calculation.
The main idea is to get rid of 52 playing cards from the big and big kings, and then randomly draw four of them. Then use the arrangement and combination to find an expression that can calculate 24.
package Poker; import ; import ; 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) { ("Poker World"); // Generate playing cards 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[(3)][(12)]; stack_temp.add(randomPoker[i]); } ("Drawn poker cards:"); for(int element: randomPoker) { ("%d ", element); } ("\n"); order(randomPoker, 4, 0); //("%d\n",count_2); if(count != 0) { ("There are %d calculation methods in total", count); } else { ("Cannot calculate 24"); } } private static void order(int[] poker, int targ, int cur) { // TODO Auto-generated method stub if(cur == targ) { String str = (); 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] = (); } for(int i = 3; i >= 0; i--) { (new_poker[i]); } do24(new_poker); } return; } for(int i = 0; i < ; i++) { if(stack_temp.contains(poker[i])) { (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_temp.add(poker[i]); } } } public static void do24(int[] poker){ // There are three operators in the middle of the four numbers, so it is traversed with a triple loop. Where 0 means +, 1 means -, 2 means x, 3 means ÷ 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); } } } } /* * Category discussion */ 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))) { ("("+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))) { (num1+symbol(op1)+"("+num2+symbol(op2)+num3+")"+symbol(op3)+num4); count++; } else { ("["+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))) { (num1+symbol(op1)+num2+symbol(op2)+"("+num3+symbol(op3)+num4+")"); count++; } else { ("("+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))) { ("("+num1+symbol(op1)+num2+symbol(op2)+num3+")"+symbol(op3)+num4); count++; } else { ("[("+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))) { (num1+symbol(op1)+"("+num2+symbol(op2)+num3+symbol(op3)+num4+")"); count++; } else { (num1+symbol(op1)+"[("+num2+symbol(op2)+num3+")"+symbol(op3)+num4+"]"); count++; } } else if(cal(num1,cal(num2,cal(num3,num4,op3),op2),op1) == 24) { (num1+symbol(op1)+"["+num2+symbol(op2)+"("+num3+symbol(op3)+num4+")]"); count++; } } /* * Calculated in order */ 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; } /* * Convert numbers representing the calculation symbol into characters and save them into String array and return */ 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; } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.