백준 잃어버린 괄호
백준 잃어버린 괄호
코드
import java.io.*;
public class Baekjoon1541 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split("-");
int ans = 0;
for(int i = 0; i<str.length;i++){
String[] str2 = str[i].split("\\+");
int sum = 0;
for(int j = 0; j<str2.length;j++){
sum+=Integer.parseInt(str2[j]);
}
if(i == 0){
ans+=sum;
}
else{
ans-=sum;
}
}
System.out.print(ans);
}
}
설명
- -토큰을 기준으로 분리
- +토큰을 기준으로 분리하여 더해준다.
- 더해준 값들을 다시 빼준다. 이 때 -토큰으로 분리한 첫번째는 더해준다.