結果

問題 No.49 算数の宿題
ユーザー tadanoningen
提出日時 2021-01-22 03:06:05
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 979 bytes
コンパイル時間 1,209 ms
コンパイル使用メモリ 68,480 KB
最終ジャッジ日時 2025-01-18 03:14:12
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>

using namespace std;

int main(){
    string s,tmp = "";
    char prev = ' ';
    cin >> s;

    int ans = 0;
    for(char c: s){
        if(c == '*'){
            if(prev == '*') ans += (int)stoi(tmp);
            if(prev == '+') ans *= (int)stoi(tmp);
            if(!(prev == '*' || prev == '+')) ans += (int)stoi(tmp);
            prev = '*';
            tmp = "";
        }else if(c == '+'){
            if(prev == '+') ans *= (int)stoi(tmp);
            if(prev == '*') ans += (int)stoi(tmp);
            if(!(prev == '*' || prev == '+')) ans += (int)stoi(tmp);
            prev = '+';
            tmp = "";
        }else tmp += c;
        //debug
        // cout << "tmp: " << tmp << endl;
        // cout << "ans: " << ans << endl;
        // cout << "prev: " << prev << endl;
        // cout << endl;
    }
    if(prev == '*') ans += (int)stoi(tmp);
    if(prev == '+') ans *= (int)stoi(tmp);
    cout << ans << endl;
    return 0;
}
0