結果

問題 No.49 算数の宿題
ユーザー LogicR_piman
提出日時 2016-10-21 23:24:48
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 893 bytes
コンパイル時間 415 ms
コンパイル使用メモリ 55,280 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-23 01:53:45
合計ジャッジ時間 981 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>

using namespace std;

int main() {
	int i, l = 0, n = 0, ans = 0, presig = 0;
	string s;

	cin >> s;

	l = s.length();

	for (i = 0; i < l + 1; i++) {
		if (s[i] == '*') {
			if (presig == 1) {
				ans += n;
				n = 0;
			}
			else if (presig == 2) {
				ans *= n;
				n = 0;
			}
			else if (presig == 0) {
				ans = n;
				n = 0;
			}
			presig = 1;
		}
		else if (s[i] == '+') {
			if (presig == 1) {
				ans += n;
				n = 0;
			}
			else if (presig == 2) {
				ans *= n;
				n = 0;
			}
			else if (presig == 0) {
				ans = n;
				n = 0;
			}
			presig = 2;
		}
		else if (s[i] == '\0') {
			if (presig == 1) {
				ans += n;
				n = 0;
			}
			else if (presig == 2) {
				ans *= n;
				n = 0;
			}
			else if (presig == 0) {
				ans = n;
				n = 0;
			}
			break;
		}
		else {
			n = n * 10 + s[i] - 48;
		}
	}
	
	cout << ans << endl;

	return 0;
}



0