結果

問題 No.193 筒の数式
ユーザー @abcde@abcde
提出日時 2019-03-02 23:41:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,696 bytes
コンパイル時間 1,673 ms
コンパイル使用メモリ 155,748 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 17:36:22
合計ジャッジ時間 2,776 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

int main() {
    
    // 1. 入力情報取得.
    string S;
    cin >> S;
    
    // 2. 数式を生成する.
    int l = S.size();
    // S を くっつけて, 筒化する.
    S += S;
    vector<int> ans;
    // 変数 i で, 計算のスタート地点とする.
    for(int i = 0; i < l; i++){
        string f = S.substr(i, l);
        // 最初と最後が演算子になってはいけないことに注意.
        if(f[0] == '+' || f[0] == '-' || f[l - 1] == '+' || f[l - 1] == '-') continue;
        // 数式を作成.
        vector<string> v;
        v.push_back({f[0]});
        for(int j = 1; j < l; j++){
            int vSize = v.size();
            string x = v[vSize - 1];
            // cout << "x=" << x << " vSize=" << vSize << endl;
            if(f[j] == '+' || f[j] == '-') v.push_back({f[j]});
            if(f[j] != '+' && f[j] != '-'){
                if(x != "+" && x != "-") v[vSize - 1] += {f[j]};
                else                     v.push_back({f[j]});
            }
        }
        // 数式を計算.
        int calc = 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 == "+") calc += num;
                if(sign == "-") calc -= num;
            }else{
                sign = p;
            }
        }
        ans.push_back(calc);
    }

    // 3. 出力.
    sort(ans.begin(), ans.end(), greater<int>());
    // for(auto &p : ans) cout << p << endl;
    cout << ans[0] << endl;
    return 0;
    
}
0