SWEA_D4_1222
π [D4_1222] κ³μ°κΈ°1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;
public class Solution{
static StringTokenizer st;
static String num;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
for (int t = 1; t <= 10; t++) {
sb.append("#").append(t).append(" ");
// κ³μ°μ κΈΈμ΄
int N = Integer.parseInt(br.readLine());
// λ¬Έμμ΄
String str = br.readLine();
Stack<String> stack = new Stack<String>();
// 첫λ²μ§Έ μ μ
λ ₯
stack.add(String.valueOf(str.charAt(0)));
for (int i = 1; i < N; i++) {
stack.add(String.valueOf(str.charAt(i)));
// μ°μ°μ λ§λ¬μ λ
if(stack.peek().equals("+")) {
stack.pop();
continue;
}
// μ«μμΈ κ²½μ°
else {
int sum = Integer.parseInt(stack.pop())+Integer.parseInt(stack.pop());
stack.add(String.valueOf(sum));
}
}
sb.append(stack.pop());
sb.append("\n");
}
System.out.println(sb);
}
}
π€ λμ μκ°
μ 체 λ¬Έμμ΄μ μμλλ‘ STACKμ λ°μμ μ°μ°μλ₯Ό λ§λλ©΄ pop νμ¬ λ°λ‘ κ³μ°ν΄μ£Όλλ‘ νμλ€.
μ΄ λ¬Έμ λ β+β λ°μ μμκΈ° λλ¬Έμ κ°λ¨νκ² νλ Έλ€.