【题目描述】

【输入格式】

【输出格式】

【样例 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
【样例 1 输出】
Yes
No
No
Yes
Yes
No
No
No
Yes
Yes
【样例 1 解释】
9 + 3 + 4 × 3 = 24
5 + 4 × 5 × 5 = 105
7 - 9 - 9 + 8 = -3
5 × 6/5 × 4 = 24
3 + 5 + 7 + 9 = 24
1 × 1 + 9 - 9 = 1
1 × 9 - 5/9 = 9
8/5 + 6 × 9 = 55
6 × 7 - 3 × 6 = 24
6 × 4 + 4/5 = 24

``````import java.util.*;

public class Test201903_2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Stack<String> num=new Stack<String>();
Stack<String> symbol=new Stack<String>();
int n=sc.nextInt();
String YN="";
sc.nextLine();
for(int i=0;i<n;i++) {
String str=sc.nextLine();
int result=0;
num.push(str.charAt(0)+"");
for(int t=0;t<5;t+=2) {
if(str.charAt(t+1)=='x'||str.charAt(t+1)=='X') {
int b1=Integer.valueOf((String) num.pop());
int b2=str.charAt(t+2)-'0';
int a=b1*b2;
result=a;
num.push(""+a);
}else if (str.charAt(t+1)=='/') {
int b1=Integer.valueOf((String) num.pop());
int b2=str.charAt(t+2)-'0';
int a=b1/b2;
result=a;
num.push(""+a);
}else {

symbol.push(str.charAt(t+1)+"");
num.push(str.charAt(t+2)+"");

}
}

while(!symbol.isEmpty()) {
if(((String)symbol.pop()).equals("+")) {
result=Integer.valueOf((String)num.pop());
int b2=Integer.valueOf((String)num.pop());
result+=b2;
num.push(result+"");

}else {
result=Integer.valueOf((String)num.pop());
int b2=Integer.valueOf((String)num.pop());
result=-result+b2;
num.push(result+"");
}

}
num.clear();
symbol.clear();

if(result!=24) {
YN+="No\n";
}else {
YN+="Yes\n";
}

}
System.out.println(YN);

}
}

``````

1个回答

7 个月之前 回复