1541
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.next();
int result = 0;
int startIndex = 0;
Loop :
for (int i = 0; i < str.length();i++) {
if (str.charAt(i) == '-') {
if (startIndex == 0) {
// 초기값 지정
result += add (str.substring(startIndex, i));
startIndex = i + 1;
}
for (int j = startIndex + 1; j < str.length(); j++) {
if (str.charAt(startIndex - 1) == '-' && str.charAt(j) == '-') {
result -= add (str.substring(startIndex, j));
startIndex = j + 1;
}
else if (i == str.length() -1) {
result -= add (str.substring(startIndex, i + 1));
break Loop;
}
}
}
else if (i == str.length() -1) {
if (startIndex > 0) {
result -= add (str.substring(startIndex , i + 1));
break;
}
if (startIndex == 0) {
result += add (str.substring(startIndex, i + 1));
break;
}
}
}
System.out.println(result);
}
static int add (String str) {
int result = 0;
int startIndex = 0;
for (int i = 0; i < str.length();i++) {
if (str.charAt(i) == '+') {
result += Integer.parseInt(str.substring(startIndex, i));
startIndex = i + 1;
}
else if (i == str.length()-1) {
result += Integer.parseInt(str.substring(startIndex, str.length()));
}
}
return result;
}
}
+
과 -
와 숫자로 구성된 식을 준다. 이 식을 괄호를 이용하여 최소값으로 표현한다.
경우의 수를 나열하면
이러한 경우의 수를 생각하여 작성하면 된다.
문제 난이도가 높지 않음에도 시간이 오래걸렸다. 머릿속으로 생각해볼때 생각보다 오래걸릴것 같은 느낌이 들면 종이에 적어보는게 좋을것 같다.