結果

問題 No.49 算数の宿題
ユーザー tetsuzuki1115tetsuzuki1115
提出日時 2018-01-15 16:13:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,549 bytes
コンパイル時間 914 ms
コンパイル使用メモリ 81,488 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 17:38:41
合計ジャッジ時間 1,646 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <math.h>
#include <cmath>
#include <limits.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <functional>
#include <stdio.h>
using namespace std;

long long MOD = 1000000007;


vector<string> split(const string &str )
{
    vector<string> v;        // 分割結果を格納するベクター
    auto first = str.begin();              // テキストの最初を指すイテレータ
    while( first != str.end() ) {         // テキストが残っている間ループ
        auto last = first;                      // 分割文字列末尾へのイテレータ
        while( last != str.end() && isdigit( *last ) )       // 末尾 or セパレータ文字まで進める
            ++last;
        v.push_back(string(first, last));       // 分割文字を出力
        if( last != str.end() )
            ++last;
        first = last;          // 次の処理のためにイテレータを設定
    }
    return v;
}

int main() {
    
    string S;
    cin >> S;
    
    vector<string> VS;
    VS = split( S );
    
    vector<int> V;
    for ( int i = 0; i < S.length(); i++ ) {
        if ( !isdigit(S[i]) ) {
            V.push_back( S[i] == '*' ? 1 : 0 );
        }
    }
    
    int ans = stoi( VS[0] );
    for ( int i = 1; i < VS.size(); i++ ) {
        int d = stoi( VS[i] ); 
        
        if ( V[i-1] ) { ans += d; }
        else { ans *= d; }
        
    }
    
    cout << ans << endl;
    
    return 0;
}
0