Yesterday took part in a company a pen test, which has a programming question is probably meaning: there is a deck of cards, remove the size of the king, there are 52 cards left, J Q K for 11,12,13. Now randomly draw four cards, each card must be used and each card can only be used once, with +-*/() to see whether the final result can be equal to 24, for example: Input 4 5 6 7, output Yes, input 1 1 2 6 output Yes, input 1 1 1 1, Output no
And now my idea is:
1. Randomly draw two numbers
2, the two number for the +-*/operation, and then put this number in the new array, the previous removed, now the array is 3 numbers
3, will then go back to 1, the recursive operation, until the N=1
4, when n=1, determine whether the result is 24, is the return true, no return false
In addition: This use of the idea of poor lifting method, and this algorithm omitted () The trouble, there should be another solution, is to use the suffix expression to complete, but I am not enough, at present did not think how to achieve, but also please friends in the following leave your code
My Code:
Import Java.util.Scanner; /** * * @author Uncle Ha * * */public class Poker24 {private static int COUNT = 4;//number of cards drawn private static Doub le[] Number = new Double[count]; Array of poker private static string[] expression = new String[count]; Expression array public static Boolean checkcondition (int n) {///when recursive to only one number, determine if the condition satisfies if (n = = 1) {if (Number[0] = = 24)
{System.out.println (expression[0].substring (1, Expression[0].length ()-1));
return true;
} else return false; } for (int i = 0, i < n-1; i++) {for (int j = i + 1; j < N; j + +) {double Tempa
= Number[i];
Double TEMPB = number[j];
String Exprea = Expression[i];
String Expreb = Expression[j];
NUMBER[J] = number[n-1];
EXPRESSION[J] = expression[n-1]; Start doing addition expression[i] = "("+exprea+ "+" +expreb+ ")";
Number[i] = TEMPA+TEMPB;
if (Checkcondition (n-1)) return true;
Subtraction, two cases, TEMPA-TEMPB and tempb-tempa expression[i] = "(" +exprea+ "-" +expreb+ ")";
Number[i] = TEMPA-TEMPB;
if (Checkcondition (n-1)) return true;
Expression[i] = "(" +expreb+ "-" +exprea+ ")";
Number[i] = Tempb-tempa;
if (Checkcondition (n-1)) return true;
Multiplication Expression[i] = "(" +exprea+ "*" +expreb+ ")";
Number[i] = TEMPA*TEMPB;
if (Checkcondition (n-1)) return true; Division, two cases, TEMPA/TEMPB and Tempb/tempa, and guarantee that the denominator is not 0 if (TEMPB! = 0) {Expression[i] = "(" +exprea
+ "/" +expreb+ ")";
Number[i] = TEMPA/TEMPB; if (Checkcondition (n-1)) RetuRN true;
} if (Tempa! = 0) {Expression[i] = "(" +expreb+ "/" +exprea+ ")";
Number[i] = Tempb/tempa;
if (Checkcondition (n-1)) return true;
} Number[i] = Tempa;
NUMBER[J] = TEMPB;
Expression[i] = Exprea;
EXPRESSION[J] = Expreb;
}} return false;
} public static void Main (string[] args) {Scanner Scanner = new Scanner (system.in);
System.out.println ("Please enter 1-13" +count+ "positive integer");
for (int i = 0; i < COUNT; i++) {Number[i] = Scanner.nextint ();
int x;
x = (int) number[i];
Expression[i] = string.valueof (x);
} if (Checkcondition (COUNT)) System.out.println ("yes");
else System.out.println ("no"); }
}