結果

問題 No.49 算数の宿題
ユーザー Shawn stayCShawn stayC
提出日時 2020-07-20 22:25:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 520 bytes
コンパイル時間 1,553 ms
コンパイル使用メモリ 165,172 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 17:48:10
合計ジャッジ時間 2,812 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 2 ms
4,376 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:10:13: 警告: ‘pos’ may be used uninitialized [-Wmaybe-uninitialized]
   10 |         int pos;
      |             ^~~
main.cpp:23:9: 警告: ‘op’ may be used uninitialized [-Wmaybe-uninitialized]
   23 |         if(op=='+') ans*=stoi(b);
      |         ^~
main.cpp:9:14: 備考: ‘op’ はここで定義されています
    9 |         char op;
      |              ^~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)

int main(){
	string s; cin>>s;
	int k=s.size(), ans=0;
	string a,b;
	char op;
	int pos;
	rep(i,k){
		if(s[i]=='+'||s[i]=='*'){pos=i+1; op=s[i]; break;}
		else a+=s[i];
	}
	ans=stoi(a);
	for(int i=pos; i<k; i++){
		b+=s[i];
		if(s[i]=='+'||s[i]=='*'){
			if(op=='+') {ans*=stoi(b); op=s[i]; b=" ";}
			if(op=='*') {ans+=stoi(b); op=s[i]; b=" ";}
		}
	}
	if(op=='+') ans*=stoi(b);
	if(op=='*') ans+=stoi(b);
	cout<<ans<<" "<<endl;
}
0