Skip to content

Commit 9ff2c04

Browse files
committed
[Stack] baekjoon-3425
1 parent 8b3aadc commit 9ff2c04

File tree

4 files changed

+16538
-0
lines changed

4 files changed

+16538
-0
lines changed

โ€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
| :-: | :-: | :----------------------------------------------------------------------------------------------------- | :--- |
3434
| 01 | | [Baekjoon-10828 ์Šคํƒ](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/Stack/P10828) | |
3535
| 02 | | [Baekjoon-2504 ๊ด„ํ˜ธ์˜ ๊ฐ’](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/Stack/P2504) | |
36+
| 03 | | [Baekjoon-3425 ๊ณ ์Šคํƒ](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/Stack/P3425) | |
3637

3738
### Queue
3839

โ€Žsrc/Stack/P3425/Main.java

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package Stack.P3425;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileInputStream;
5+
import java.io.InputStreamReader;
6+
import java.util.ArrayList;
7+
import java.util.Stack;
8+
9+
public class Main {
10+
static Program pro;
11+
12+
public static void main(String[] args) throws Exception {
13+
System.setIn(new FileInputStream("src/Stack/P3425/input.txt"));
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
16+
String cmd = br.readLine();
17+
while (!cmd.equals("QUIT")) {
18+
ArrayList<String> commands = new ArrayList<>();
19+
20+
while(!cmd.equals("END")) {
21+
commands.add(cmd);
22+
cmd = br.readLine();
23+
}
24+
25+
int N = Integer.parseInt(br.readLine());
26+
for (int i = 0; i < N; i++) {
27+
int num = Integer.parseInt(br.readLine());
28+
pro = new Program(num, commands);
29+
}
30+
System.out.println("");
31+
32+
br.readLine();
33+
cmd = br.readLine();
34+
}
35+
}
36+
}
37+
38+
class Program {
39+
Stack<Integer> stack = new Stack<>();
40+
final int MAX = 1000000000;
41+
boolean error = false;
42+
43+
public Program(int num, ArrayList<String> commands) {
44+
this.stack.push(num);
45+
for (String cmd : commands) {
46+
if (this.error) break;
47+
48+
if (cmd.matches(".*[0-9]")) {
49+
this.num(Integer.parseInt(cmd.substring(4)));
50+
} else {
51+
switch (cmd) {
52+
case "POP":
53+
this.pop();
54+
break;
55+
case "INV":
56+
this.inv();
57+
break;
58+
case "DUP":
59+
this.dup();
60+
break;
61+
case "SWP":
62+
this.swp();
63+
break;
64+
default:
65+
this.oper(cmd);
66+
break;
67+
}
68+
}
69+
}
70+
71+
if (this.error || stack.size() != 1) System.out.println("ERROR");
72+
else System.out.println(stack.pop());
73+
}
74+
75+
void num(int x) {
76+
this.stack.push(x);
77+
}
78+
79+
void pop() {
80+
if (this.validSize(1)) this.stack.pop();
81+
}
82+
83+
void inv() {
84+
if (!this.validSize(1)) return;
85+
int target = this.stack.pop();
86+
this.stack.push(target * (-1));
87+
}
88+
89+
void dup() {
90+
if (!this.validSize(1)) return;
91+
int target = this.stack.peek();
92+
this.stack.push(target);
93+
}
94+
95+
void swp() {
96+
if (!this.validSize(2)) return;
97+
int first = this.stack.pop();
98+
int second = this.stack.pop();
99+
this.stack.push(first);
100+
this.stack.push(second);
101+
}
102+
103+
void oper(String o) {
104+
if (!this.validSize(2)) return;
105+
int first = this.stack.pop();
106+
int second = this.stack.pop();
107+
switch (o) {
108+
case "ADD":
109+
this.add(second, first);
110+
break;
111+
case "SUB":
112+
this.sub(second, first);
113+
break;
114+
case "MUL":
115+
this.mul(second, first);
116+
break;
117+
case "DIV":
118+
this.div(second, first);
119+
break;
120+
case "MOD":
121+
this.mod(second, first);
122+
break;
123+
default:
124+
break;
125+
}
126+
}
127+
128+
void add(int x, int y) {
129+
long result = x + y;
130+
if (this.validNum(result)) this.stack.push((int) result);
131+
}
132+
133+
void sub(int x, int y) {
134+
long result = x - y;
135+
if (this.validNum(result)) this.stack.push((int) result);
136+
}
137+
138+
void mul(int x, int y) {
139+
if (x != 0 && y!= 0) {
140+
int x_len = (int) Math.log10(x) + 1;
141+
int y_len = (int) Math.log10(y) + 1;
142+
if (x_len + y_len > 10) {
143+
this.error = true;
144+
return;
145+
}
146+
}
147+
long result = x * y;
148+
if (this.validNum(result)) this.stack.push((int) result);
149+
}
150+
151+
void div(int x, int y) {
152+
if (y == 0) {
153+
this.error = true;
154+
return;
155+
}
156+
long result = Math.abs(x) / Math.abs(y);
157+
if ((x < 0 && y > 0) || (x > 0 && y < 0)) result *= -1;
158+
if (this.validNum(result)) this.stack.push((int) result);
159+
}
160+
161+
void mod(int x, int y) {
162+
if (y == 0) {
163+
this.error = true;
164+
return;
165+
}
166+
long result = Math.abs(x) % Math.abs(y);
167+
if (x < 0) result *= -1;
168+
if (this.validNum(result)) this.stack.push((int) result);
169+
}
170+
171+
boolean validSize(int targetSize) {
172+
if (this.stack.size() < targetSize) {
173+
this.error = true;
174+
return false;
175+
}
176+
return true;
177+
}
178+
179+
boolean validNum(long target) {
180+
if (Math.abs(target) > this.MAX) {
181+
this.error = true;
182+
return false;
183+
}
184+
return true;
185+
}
186+
}

โ€Žsrc/Stack/P3425/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [baekjoon-3425] ๊ณ ์Šคํƒ
2+
3+
![image](https://user-images.githubusercontent.com/22045163/91573791-f513f400-e981-11ea-89a1-aa0f67ac797e.png)

0 commit comments

Comments
ย (0)