Java/백준
[Java][10828] 스택
tmd1
2022. 7. 23. 19:29
문제:
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
코드:
import java.util.*;
import java.io.*;
public class Main {
public static int MX = 10001;
public static int [] dat = new int[MX];
public static int pos = 0;
public static void push(int x) { // 스택에 값 추가
dat[pos++] = x;
}
public static void pop() { // 꼭대기 원소 제거
if(pos != 0) {
System.out.println(dat[pos-1]);
dat[pos-1] = 0;
pos--;
}else {
System.out.println(-1);
}
}
public static void top() { // 꼭대기 값 학인
if(pos != 0) {
System.out.println(dat[pos-1]);
}else{
System.out.println(-1);
}
}
public static int size() {
int cnt = 0;
for(int i=0;i<10001;i++) {
if(dat[i] != 0) {
cnt++;
}
}
return cnt;
}
public static void empty() {
if(dat[0] == 0) {
System.out.println(1);
}else {
System.out.println(0);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for(int i=0;i<n;i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
String commend = st.nextToken();
if(commend.equals("push")) {
int val = Integer.parseInt(st.nextToken());
push(val);
}
if(commend.equals("pop")) {
pop();
}
if(commend.equals("size")) {
System.out.println(size());
}
if(commend.equals("empty")) {
empty();
}
if(commend.equals("top")) {
top();
}
}
}
}
위 직접 구현
아래 코드 참고 구현
import java.util.*;
import java.io.*;
public class Main {
public static int MX = 10001;
public static int [] dat = new int[MX];
public static int pos = 0;
public static void push(int x) { // 스택에 값 추가
dat[pos++] = x;
}
public static void pop() { // 꼭대기 원소 제거
pos--;
}
public static int top() { // 꼭대기 값 학인
return dat[pos-1];
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for(int i=0;i<n;i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
String commend = st.nextToken();
switch(commend) {
case "push" :
int val = Integer.parseInt(st.nextToken());
push(val);
break;
case "pop" :
if(pos == 0) {
System.out.println(-1);
}else {
System.out.println(top());
pop();
}
break;
case "size" :
System.out.println(pos);
break;
case "empty" :
if(pos == 0) {
System.out.println(1);
}else {
System.out.println(0);
}
break;
case "top" :
if(pos == 0) {
System.out.println(-1);
}else {
System.out.println(top());
}
break;
}
/*
if(commend.equals("push")) {
int val = sc.nextInt();
push(val);
}
if(commend.equals("pop")) {
if(pos == 0) {
System.out.println(-1);
}else {
System.out.println(top());
pop();
}
}
if(commend.equals("size")) {
System.out.println(pos);
}
if(commend.equals("empty")) {
if(pos == 0) {
System.out.println(1);
}else {
System.out.println(0);
}
}
if(commend.equals("top")) {
if(pos == 0) {
System.out.println(-1);
}else {
System.out.println(top());
}
}*/
}
}
}
scanner 대신 쓸 buffer에 관한 공부 필요
참고
https://github.com/encrypted-def/basic-algo-lecture/blob/master/0x05/solutions/10828.cpp
GitHub - encrypted-def/basic-algo-lecture: 바킹독의 실전 알고리즘 강의 자료
바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.
github.com