• 문제 링크
9843번: LVM
The first line of the input contains an integer n between 2 and 1000, indicating the number of instructions of the program. The following n lines contain the program instructions. Arguments are separated by a space character from the preceding instruction.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
Pair[] arr = new Pair[n];
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
arr[i] = new Pair(st.nextToken(), st.hasMoreTokens() ? Integer.parseInt(st.nextToken()) : 0);
}
Stack<Integer> stk = new Stack<>();
int reg = 0;
label:
for (int i = 0; i < n; i++) {
switch (arr[i].s) {
case "PUSH":
stk.push(arr[i].i);
break;
case "STORE":
reg = stk.pop();
break;
case "LOAD":
stk.push(reg);
break;
case "PLUS":
stk.push(stk.pop() + stk.pop());
break;
case "TIMES":
stk.push(stk.pop() * stk.pop());
break;
case "IFZERO":
if (stk.pop() == 0) i = arr[i].i - 1;
break;
default:
break label;
}
}
bw.write(String.valueOf(stk.peek()));
bw.flush();
}
private static class Pair {
String s;
int i;
Pair(String s, int i) {
this.s = s;
this.i = i;
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 6601 Knight Moves - Graph Theory / Java (0) | 2024.01.05 |
---|---|
[백준] 8975 PJESMA - Data Structure / Java (0) | 2024.01.04 |
[백준] 6119 Cow Line - Data Structure / Java (0) | 2024.01.02 |
[백준] 2371 파일 구별하기 - Data Structure / Java (0) | 2024.01.01 |
[백준] 15323 ZigZag - Data Structure / Java (0) | 2023.12.31 |
댓글