結果

問題 No.708 (+ー)の式
ユーザー EsireEsire
提出日時 2018-11-04 17:14:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,658 bytes
コンパイル時間 695 ms
コンパイル使用メモリ 88,448 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-13 03:23:35
合計ジャッジ時間 1,439 ms
ジャッジサーバーID
(参考情報)
judge14 / 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 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
g++ -std=c++11 -Wall -O2 -o main.exe main.cpp
./main.exe
*/

#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <climits>
//intの最大値: INT_MAX
//longの最大値: LONG_MAX
//long longの最大値: LLONG_MAX
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <functional>
#include <algorithm>
#include <complex>
#include <numeric>
#include <queue>
#include <stack>
//最大公約数: gcd()
//最小公倍数: lcm()

#define ll long long int

using namespace std;

template <typename T>
void sortasc(vector<T> &v){ //vectorを昇順にソート
    sort(v.begin(), v.end(), std::less<T>());
    return;
}

template <typename T>
void sortdesc(vector<T> &v){ //vectorを降順にソート
    sort(v.begin(), v.end(), std::greater<T>());
    return;
}

//------------------------------------------------------------------------------

string S;
int index;

int k(){
	int val = 0;
	char t;

	while(1){
		t = S[index++];

		if(t == ')'){
			return val;
		}
		else if(t == '+'){
			val += (int)(S[index++] - '0');
		}
		else if(t == '-'){
			val -= (int)(S[index++] - '0');
		}
		else{
			val += (int)(t - '0');
		}
	}
}

int main(){
	cin >> S;

	int val = 0;
	char t;

	index = 0;
	
	while(index < (int)(S.size())){
		t = S[index++];

		if(t == '+'){
			if(S[index] == '('){
				index++;
				val += k();
			}
			else val += (int)(S[index++] - '0');
		}
		else if(t == '-'){
			if(S[index] == '('){
				index++;
				val -= k();
			}
			else val -= (int)(S[index++] - '0');
		}
		else if(t == '('){
			val += k();
		}
		else{
			val += (int)(t - '0');
		}

	}

	cout << val << endl;

	return 0;
}
0