結果

問題 No.49 算数の宿題
ユーザー kichirb3kichirb3
提出日時 2018-03-10 01:00:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,170 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 78,052 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 17:38:53
合計ジャッジ時間 1,346 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// No.49 算数の宿題
// https://yukicoder.me/problems/no/49
//

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int solve(string &S);
pair<vector<int>, vector<char>> separate_string(string &S);


int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    string S;
    cin >> S;
    int ans = solve(S);
    cout << ans << endl;
}

int solve(string &S) {
    pair<vector<int>, vector<char>> res = separate_string(S);
    vector<int>  numbers = res.first;
    vector<char> ops = res.second;

    int ans = numbers[0];
    for (auto i = 0; i < numbers.size()-1; ++i) {
        if (ops[i] == '*')
            ans += numbers[i+1];
        else
            ans *= numbers[i+1];
    }
    return ans;
}

pair<vector<int>, vector<char>> separate_string(string &S) {
    vector<int> numbers;
    vector<char> ops;
    string t;

    for (char c: S) {
        if (c == '*' || c == '+') {
            numbers.push_back(stoi(t));
            ops.push_back(c);
            t = "";
        } else {
            t += c;
        }
    }
    numbers.push_back(stoi(t));
    return make_pair(numbers, ops);
}
0