Dot Game 1. Problem Description:
Give the player 4 cards, the face value of each card in 1-13, allowing the same number of cards, the use of addition, subtraction, multiplication, except arithmetic, allowing the intermediate operation of decimals, and can use parentheses, but only once per card. Constructs an expression so that the result is 24.
Input: N1, N2, N3, N4 (1~13)
Output: If you can get the result of the operation is 24, then output a corresponding operation expression
Such as:
Input: 11, 8, 3, 5
Output: (11-8) * (3*5) = 24
2. "Solution One" poor lifting method
The code is as follows:
1 PackageChapter1youxizhilepointsgame;2 3 ImportJava.util.Scanner;4 5 /**6 * Point Game (24 points)7 * "solution one" poor lifting method8 * @authorDELL9 *Ten */ One Public classPointsGame1 { A Public Static Final DoublePrecision = 1E-6;//accuracy - Public Static Final intCardsnumber = 4;//Number of Cards - Public Static Final intResultvalue = 24;//required result value of the Operation the Private Double[] number;//all possible arithmetic the resulting value, initially the value of the card - PrivateString[] result;//Store the arithmetic result string to output - //constructors Initialize an array of arrays - PublicPointsGame1 () { +Number =New Double[Cardsnumber]; -result =NewString[cardsnumber]; +Scanner input =NewScanner (system.in); ASystem.out.print ("Please enter the value of the" +cardsnumber+ "card (separated by a space):"); at for(inti=0;i<cardsnumber;i++){ -Number[i] =input.nextint (); -Result[i] =double.tostring (Number[i]); - } - - } in /** - * The function of the exhaustive calculation to * @paramn Arithmetic number of results, initial number of cards + * @returncan I find the corresponding op-expression - */ the Public BooleanPointsgame (intN) { * if(n==1){ $ if(Math.Abs (Number[0]-resultvalue) <=Precision) {Panax NotoginsengSystem.out.println ("The operation expression satisfying the condition is:" +result[0]); - return true; the}Else{ + return false; A } the } + for(inti=0;i<n;i++){ - for(intj=i+1;j<n;j++){ $ Doubleb;//two numbers currently taken out $String EXPA,EXPB;//the currently stored arithmetic string -A =Number[i]; -b =Number[j]; theNUMBER[J] = number[n-1]; - WuyiEXPA =Result[i]; theEXPB =Result[j]; -RESULT[J] = result[n-1]; Wu //addition Operation -Number[i] = a +b; AboutResult[i] = "(" +expa+ "+" +expb+ ")"; $ if(Pointsgame (n-1)) - return true; - //Subtraction Operations -Number[i] = A-b; AResult[i] = "(" +expa+ "-" +expb+ ")"; + if(Pointsgame (n-1)) the return true; - $Number[i] = B-A; theResult[i] = "(" +expb+ "-" +expa+ ")"; the if(Pointsgame (n-1)) the return true; the //Multiplication Operations -Number[i] = A *b; inResult[i] = "(" +expa+ "*" +expb+ ")"; the if(Pointsgame (n-1)) the return true; About //Division Operation the if(b!=0){ theNumber[i] = A/b; theResult[i] = "(" +expa+ "/" +expb+ ")"; + if(Pointsgame (n-1)) - return true; the }Bayi the if(a!=0){ theNumber[i] = b/A; -Result[i] = "(" +expb+ "/" +expa+ ")"; - if(Pointsgame (n-1)) the return true; the } the //If you haven't found it, go back to the original state and proceed to the next round of searches. theNumber[i] =A; -NUMBER[J] =b; theResult[i] =Expa; theRESULT[J] =EXPB; the }94 } the return false; the } the 98 Public Static voidMain (string[] args) { AboutPointsGame1 pg =NewPointsGame1 (); - if(Pg.pointsgame (Cardsnumber)) {101System.out.println ("Construction succeeded! ");102}Else103System.out.println ("Construction failed! ");104 } the 106}
The results of the program run as follows:
Please enter a value of 4 cards (separated by spaces): 8 3 5 The expression that satisfies the condition is: (((11.0-8.0) +5.0) *3.0) constructed successfully!
"Method Two" reference Link: Programming Beauty-24 points game programming beauty 24 points game sentiment "The beauty of programming" Reading notes 22:1.16 24 points Game
The 1th chapter game music-point game