結果

問題 No.49 算数の宿題
ユーザー @abcde@abcde
提出日時 2019-03-05 23:41:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,223 bytes
コンパイル時間 1,390 ms
コンパイル使用メモリ 150,880 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 17:41:14
合計ジャッジ時間 1,908 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    
    // 1. 入力情報取得.
    string S;
    cin >> S;
    
    // 2. 数式を生成する.
    int l = S.size();
    vector<string> v;
    v.push_back({S[0]});
    // 変数 i で, 計算のスタート地点とする.
    for(int i = 1; i < l; i++){
        string f = S.substr(i, 1);
        int vSize = v.size();
        string x = v[vSize - 1];
        // cout << "x=" << x << " vSize=" << vSize << endl;
        if(f == "+" || f == "*") v.push_back(f);
        if(f != "+" && f != "*"){
            if(x != "+" && x != "*") v[vSize - 1] += f;
            else                     v.push_back(f);
        }
    }

    // 数式を計算.
    int ans = stoi(v[0]);
    string sign = "";
    for(int i = 1; i < v.size(); i++){
        string p = v[i];
        // cout << "p=" << p << endl;
        if(p != "+" && p != "*"){
            int num = stoi(p);
            // 太郎君の国は, 足し算: "*", 掛け算: "+" で表されるとのこと.
            if(sign == "+") ans *= num;
            if(sign == "*") ans += num;
        }else{
            sign = p;
        }
    }
    
    // 3. 出力.
    cout << ans << endl;
    return 0;
    
}
0