結果

問題 No.49 算数の宿題
ユーザー face4
提出日時 2018-05-03 16:39:03
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 935 bytes
コンパイル時間 921 ms
コンパイル使用メモリ 74,624 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-23 01:59:05
合計ジャッジ時間 1,322 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<stack>

using namespace std;

int main(){
    int ans = 0;
    string s;
    cin >> s;

    stack<string> a;

    for(int index = 0; index < s.length();){
        if(s[index] == '*' || s[index] == '+'){
            string o{s[index++]};
            a.push(o);
        }else{
            string x = "";        
            while(s[index] != '*' && s[index] != '+'){
                x += s[index++];
            }
            if(a.size() == 2){
                string ope = a.top(); a.pop();
                int first = stoi(a.top());  a.pop();
                if(ope == "+"){
                    a.push(to_string( (first * stoi(x)) ));
                }else if(ope == "*"){
                    a.push(to_string( (first + stoi(x)) ));
                }
            }else{
                a.push(x);            
            }
        }
    }

    cout << stoi(a.top()) << endl;
    return 0;
}
0