結果

問題 No.49 算数の宿題
ユーザー tadanoningen
提出日時 2021-01-22 03:02:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 973 bytes
コンパイル時間 518 ms
コンパイル使用メモリ 68,480 KB
最終ジャッジ日時 2025-01-18 03:14:09
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 2 WA * 8
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:20:13: warning: ‘prev’ may be used uninitialized [-Wmaybe-uninitialized]
   20 |             if(prev == '+') ans *= (int)stoi(tmp);
      |             ^~
main.cpp:8:10: note: ‘prev’ was declared here
    8 |     char prev;
      |          ^~~~

ソースコード

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