#46136: yep 小蜜楓-->EC


1121226@stu.wghs.tp.edu.tw (Arthur✨EC)

學校 : 臺北市私立薇閣高級中學
編號 : 252772
來源 : [60.248.154.139]
最後登入時間 :
2025-08-21 12:59:45

#include <bits/stdc++.h>
using namespace std;
int main() {
    string line;
    getline(cin,line);  // 讀取整行
    istringstream iss(line);
    stack<long long> stk;
    string token;
    while (iss>>token){
        // 判斷是否為運算子或數值
        if (token=="+"||token == "-"||token=="*"||token=="/"){
            // C++14 以上,避免除以 0
            long long b=stk.top(); stk.pop();
            long long a=stk.top(); stk.pop();
            long long res;
            if (token=="+"){
                res=a+b;
            }
            else if (token=="-"){
                res=a-b;
            }
            else if (token=="*"){
                res=a*b;
            }
            else{ // division
                // 根據題意,運算結果為 32 位元有號整數,且除數不會為 0(題未說明檢查,但一般預設)
                res=a/b;
            }
            stk.push(res);
        } 
        else {
            // 嘗試轉成整數
            long long num=stoll(token);
            stk.push(num);
        }
    }

    // 最終堆疊中剩下的值即為運算結果
    cout<<stk.top()<<endl;
    return 0;
}