Python怎么这么香(洛谷P2788题解,Java语言描述)
生活随笔
收集整理的這篇文章主要介紹了
Python怎么这么香(洛谷P2788题解,Java语言描述)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目要求
P2788題目鏈接
分析
表達式求值而已,無營養題目~~
用Java簡單求解即可
AC代碼(Java語言描述)
import java.util.LinkedList; import java.util.Queue; import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String expression = scanner.nextLine();scanner.close();char[] chars = expression.toCharArray();Queue<Character> queue = new LinkedList<>();for (char c : chars) {if (c == '+' || c == '-') {queue.offer(c);}}String[] numArray = expression.split("[+]|[-]");long result = Integer.parseInt(numArray[0]);for (int i = 1; i < numArray.length; i++) {long temp = Integer.parseInt(numArray[i]);if (queue.poll() == '+') {result += temp;} else {result -= temp;}}System.out.println(result);} }Python它不香嗎
香香香,怎么可能不香呢~~
Life is short, you need Python!
你知道Python怎么解這個題嗎?
- Python2: print(input())
- Python3: print(eval(input()))
沒錯,這就完事,香不香~~~
總結
以上是生活随笔為你收集整理的Python怎么这么香(洛谷P2788题解,Java语言描述)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 更正日期(洛谷P5690题题解,Java
- 下一篇: 地图上探测器扫描到的范围(洛谷P3717