acmicpc

[BAEKJOON] 1541. 잃어버린 괄호

코드와이 2021. 3. 7. 16:07

 

문제링크

www.acmicpc.net/problem/1541

 

1541번: 잃어버린 괄호

첫째 줄에 식이 주어진다. 식은 ‘0’~‘9’, ‘+’, 그리고 ‘-’만으로 이루어져 있고, 가장 처음과 마지막 문자는 숫자이다. 그리고 연속해서 두 개 이상의 연산자가 나타나지 않고, 5자리보다

www.acmicpc.net

 

package acmicpc.Silver2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class 잃어버린_괄호 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String str = br.readLine();
		List<String> list = new ArrayList<String>();
		
		int i = 0;
		while(true) {
			String s = "";
			while(i < str.length() && str.charAt(i) != '+' && str.charAt(i) != '-') {
				s += str.substring(i, i+1);
				i++;
			}
			list.add(s);
			if(i == str.length()) break;
			list.add(str.substring(i, ++i));
		}
		
		int ans = 0;
		int sum = 0;
		for(i = list.size() - 1; i >= 0 ; i--) {
			if(!list.get(i).equals("+") &&!list.get(i).equals("-")) {
				sum += Integer.parseInt(list.get(i));				
			}
			else if (list.get(i).equals("-")) {
				ans -= sum;
				sum = 0;
			}
		}
		ans += sum;
		System.out.println(ans);
	}
}