초보개발자 긍.응.성
[Java] 1541. 잃어버린 괄호 본문
반응형
1541번 잃어버린 괄호
문제출저: https://www.acmicpc.net/problem/1541
StringTokenizer와 Integer.parseInt를 이용해 해결한 문제
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
br.close();
StringTokenizer st = new StringTokenizer(line, "-");
int num = evalPlusEquation(st.nextToken());
while (st.hasMoreTokens()) {
num -= evalPlusEquation(st.nextToken());
}
System.out.println(num);
}
private static int evalPlusEquation(String plusEq) {
StringTokenizer plusNums = new StringTokenizer(plusEq, "+");
int num = 0;
while (plusNums.hasMoreTokens()) {
num += Integer.parseInt(plusNums.nextToken());
}
return num;
}
}
반응형
Comments