백준 1541번 (잃어버린 괄호, C++) [BAEKJOON]

잃어버린 괄호

https://www.acmicpc.net/problem/1541

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초128 MB60829317652520151.706%

문제

세준이는 양수와 +, -, 그리고 괄호를 가지고 식을 만들었다.

그리고 나서 세준이는 괄호를 모두 지웠다.

그리고 나서 세준이는 괄호를 적절히 쳐서 이 식의 값을 최소로 만들려고 한다.

괄호를 적절히 쳐서 이 식의 값을 최소로 만드는 프로그램을 작성하시오.

입력

첫째 줄에 식이 주어진다. 식은 ‘0’~‘9’, ‘+’, 그리고 ‘-’만으로 이루어져 있고, 가장 처음과 마지막 문자는 숫자이다.

그리고 연속해서 두 개 이상의 연산자가 나타나지 않고, 5자리보다 많이 연속되는 숫자는 없다.

수는 0으로 시작할 수 있다. 입력으로 주어지는 식의 길이는 50보다 작거나 같다.

출력

첫째 줄에 정답을 출력한다.

예제 입력 1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
55-50+40
55-50+40
55-50+40

예제 출력 1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
-35
-35
-35

예제 입력 2

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
10+20+30+40
10+20+30+40
10+20+30+40

예제 출력 2

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
100
100
100

예제 입력 3

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
00009-00009
00009-00009
00009-00009

예제 출력 3

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0
0
0

출처

알고리즘 분류


통과된 코드

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
#include <cmath>
using namespace std;
string str, tempStr;
int result = 0;
bool check = false;
int main()
{
ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
cin.tie(NULL);
cout.tie(NULL);
cin >> str;
// 한번이라도 - 가 나온다면 뒤의 수들은 전부 뺴준다.
for (int i = 0; i < str.length(); i++) {
if (str[i] == 45) { // '-' = 45
result += atoi(tempStr.c_str()) * ( pow(-1, check));
check = true;
tempStr = "";
}
else if (str[i] == 43) { // '+' = 43
result += atoi(tempStr.c_str()) * (pow(-1, check));
tempStr = "";
}
else tempStr.push_back(str[i]);
}
// 마지막 숫자 해결
result += atoi(tempStr.c_str()) * (pow(-1, check));
cout << result;
return 0;
}
#include <iostream> #include <cmath> using namespace std; string str, tempStr; int result = 0; bool check = false; int main() { ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화 // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다. cin.tie(NULL); cout.tie(NULL); cin >> str; // 한번이라도 - 가 나온다면 뒤의 수들은 전부 뺴준다. for (int i = 0; i < str.length(); i++) { if (str[i] == 45) { // '-' = 45 result += atoi(tempStr.c_str()) * ( pow(-1, check)); check = true; tempStr = ""; } else if (str[i] == 43) { // '+' = 43 result += atoi(tempStr.c_str()) * (pow(-1, check)); tempStr = ""; } else tempStr.push_back(str[i]); } // 마지막 숫자 해결 result += atoi(tempStr.c_str()) * (pow(-1, check)); cout << result; return 0; }
#include <iostream>
#include <cmath>

using namespace std;

string str, tempStr;

int result = 0;
bool check = false;

int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> str;

	// 한번이라도 - 가 나온다면 뒤의 수들은 전부 뺴준다.
	for (int i = 0; i < str.length(); i++) {
		if (str[i] == 45) { // '-' = 45
			result += atoi(tempStr.c_str()) * ( pow(-1, check));
			check = true;
			tempStr = "";
		}
		else if (str[i] == 43) { // '+' = 43
			result += atoi(tempStr.c_str()) * (pow(-1, check));
			tempStr = "";
		}
		else tempStr.push_back(str[i]);
	}

	// 마지막 숫자 해결
	result += atoi(tempStr.c_str()) * (pow(-1, check));

	cout << result;

	return 0;
}

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다