本来想用后缀表达式,但是感觉代码太长了,算式也不复杂就懒得写,就模拟了下 过程,思路很简单,算两趟,第一次算乘除,第二次算加减
import java.util.LinkedList; import java.util.Scanner; /** * @Auther: Pengwen * @Date: 2019/12/8 21:29 * @Description: */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); LinkedList<Character> op = new LinkedList<>(); LinkedList<Integer> number = new LinkedList<>(); String str; while (n-- != 0) { char[] chars = scanner.next().toCharArray(); for (int i = 0; i < chars.length; i++) { switch (chars[i]) { case ‘x‘: { number.add(number.removeLast() * (chars[i + 1] - ‘0‘)); i++; break; } case ‘/‘: { number.add(number.removeLast() / (chars[i + 1] - ‘0‘)); i++; break; } case ‘+‘: { op.add(chars[i]); break; } case ‘-‘: { op.add(chars[i]); break; } default: { number.add(chars[i] - ‘0‘); } } } for (Character aChar : op) { if (aChar == ‘+‘) { number.addFirst(number.removeFirst() + number.removeFirst()); } else { number.addFirst(number.removeFirst() - number.removeFirst()); } } if(number.getFirst()==24) System.out.println("Yes"); else System.out.println("No"); number.clear(); op.clear(); } } }
原文地址:https://www.cnblogs.com/pw126046/p/12008604.html
时间: 2024-11-02 10:36:35