201903-2 24.2
[Title] background
24.2 is a famous card game, whose goal is to use the three games arithmetic operation so that the operation result on four card numbers is 24.
[Title Description
define each game by 4 from 1-9 digits and three four operators composed ensure four operators digital twenty-two spaced, brackets and other characters do not exist, the order of operations in accordance with four order of operations . Wherein the addition symbol + indicates, the subtraction by the symbol - represents a multiplication by lowercase letters x , said division by the symbol / represents. Division is divisible in the game, for example, 2/3 = 0,3 / 2 = 4/2 = 2 .
The teacher gave you the solution n a game, you write a program to verify whether the results of each game is 24.
[Input format
from standard input data.
A first line of the input integer n, the second from the beginning to the line n + 1 rows, each row containing a string of length 7, 24 points for the above-described game, to ensure valid data format.
[] Output format
to standard output.
N rows, for each - a game, if the result is Yes output string 24, or the output string No.
Sample input [1]
10
. 9. 3 + 4x3 +
24.2 is a famous card game, whose goal is to use the three games arithmetic operation so that the operation result on four card numbers is 24.
[Title Description
define each game by 4 from 1-9 digits and three four operators composed ensure four operators digital twenty-two spaced, brackets and other characters do not exist, the order of operations in accordance with four order of operations . Wherein the addition symbol + indicates, the subtraction by the symbol - represents a multiplication by lowercase letters x , said division by the symbol / represents. Division is divisible in the game, for example, 2/3 = 0,3 / 2 = 4/2 = 2 .
The teacher gave you the solution n a game, you write a program to verify whether the results of each game is 24.
[Input format
from standard input data.
A first line of the input integer n, the second from the beginning to the line n + 1 rows, each row containing a string of length 7, 24 points for the above-described game, to ensure valid data format.
[] Output format
to standard output.
N rows, for each - a game, if the result is Yes output string 24, or the output string No.
Sample input [1]
10
. 9. 3 + 4x3 +
5+4x5x5
7-9-9+8
5x6/5x4
3+5+7+9
1x1+9-9
1x9-5/9
8/5+6x9
6x7-3x6
6x4 + 4/5 [output] Sample 1
Yes
No
No
Yes
Yes
No
No
No
Yes
Yes
Sample 1 [explain]
9 + 3 + 4x3 = 24
5+4x5x5=105
7-9-9+8=-3
5x6/5x4=24
3+5+7+9=24
1x1+9-9=1
1x9-5/9=9
8/5+6x9=55
6x7-3x6=24
6x4+4/5=24
import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); while (n > 0) { String str = sc.nextLine(); Stack<Integer> number = new Stack<>(); char[] ch = str.toCharArray(); int sum = 0; int i = 0; /* * for (char c : ch) { System.out.println(c); } */ while (i < 7) { if (i == 0) { number.push(ch[i] - '0'); i++; }else { if (ch[i] == 'x') { i++; int left = number.pop(); number.push(left * (ch[i] - '0')); i++; //System.out.println("xxxxxxxx"); }else { if (ch[i] == '/') { i++; int left = number.pop(); number.push(left / (ch[i] - '0')); i++; //System.out.println("///////////"); }else { if (ch[i] == '-') { i++; number.push(-(ch[i] - '0')); i++; // System.out.println("--------------"); }else { i++; number.push(ch[i] - '0'); i++; // System.out.println("+++++++++++"); } } } } } sum = number.pop(); //System.out.println(sum); while (!number.empty()) { int right = number.pop(); sum += right; // System.out.println(right); // System.out.println(sum); } // System.out.println(sum); if (sum == 24) { System.out.println("Yes"); } else { System.out.println("No"); } n--; } } }