結果

問題 No.708 (+ー)の式
ユーザー aaaaaa
提出日時 2018-07-21 17:38:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,736 bytes
コンパイル時間 840 ms
コンパイル使用メモリ 97,332 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-27 00:41:18
合計ジャッジ時間 1,584 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <map>
#include <iomanip>
#include <math.h> 
#include <stack>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <tuple>
#include <cctype>

using namespace std;

int func(string s) {
	int num = 0;
	string fugo = "+";

	for (int i = 0; i < s.length(); i++) {
		if (s[i] == '+' || s[i] == '-') {
			fugo = s[i];
		}
		else {
			if (fugo == "+") {
				if (s[i + 1] != '+' && s[i + 1] != '-' && (int)s[i + 1] - 48 >= 0 && (int)s[i + 1] - 48 <= 9) {
					num += stoi(s.substr(i, 2));
					i++;
				}
				else {
					num += (int)s[i] - 48;
				}
			}
			else {
				if (s[i + 1] != '+' && s[i + 1] != '-' && (int)s[i + 1] -48 >= 0 && (int)s[i + 1] - 48 <= 9) {
					num -= stoi(s.substr(i, 2));
					i++;
				}
				else {
					num -= (int)s[i] - 48;
				}
			}
		}
	}




	return num;
}


int main() {
	int i, j, k;
	string s;
	string kako[100];
	int cnt = 0;

	cin >> s;


	for (i = 0; i < s.length(); i++) {
		if (s[i] == '(') {
			for (j = i + 1; j < s.length(); j++) {
				if (s[j] == ')') {
					kako[cnt] = s.substr(i + 1, j - (i + 1));
					int num = func(kako[cnt]);
					cnt++;
					if (num < 0) {
						if (s[i - 1] == '+') {
							s.replace(i-1, j - i + 2, to_string(num));
						}
						else if ( s[i - 1] == '-') {
							num = abs(num);
							s[i - 1] = '+';
							s.replace(i, j - i + 1, to_string(num));
						}
						else {
							s.replace(i, j - i + 1, to_string(num));
						}
					}
					else {
						s.replace(i, j - i + 1, to_string(num));
					}
					i = -1;
					break;
				}
			}
		}
	}

	int sum = func(s);

	cout << sum << endl;









	getchar();
	getchar();
	return 0;
}
0