CS/Algorithm
백준 10828
KJY
2021. 5. 17. 00:24
문제
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
사용 언어
javaScript
풀이
// let fs = require('fs');
// let input = fs.readFileSync('/dev/stdin').toString().split('\n');
const input = ['14', 'push 1', 'push 2', 'top', 'size', 'empty', 'pop', 'pop', 'pop', 'size', 'empty', 'pop', 'push 3', 'empty', 'top'];
let stack = [];
counter = Number(input[0]);
for (i = 1; i < counter + 1; i++) {
const comand = input[i].split(' ');
if (comand.length == 1){
// pop 일때
if (comand[0] === 'pop') {
if (stack.length == 0) {
console.log(-1)
} else {
console.log(stack.pop())
}
}
// top 일때
if (comand[0] === 'top') {
if (stack.length == 0) {
console.log(-1);
} else {
console.log(stack[stack.length -1])
}
}
// empty 일때
if (comand[0] === 'empty') {
if (stack.length == 0) {
console.log(1)
} else {
console.log(0)
}
}
// size 일때
if (comand[0] === 'size') {
console.log(stack.length)
}
} else {
// push 일때
x = comand[1];
stack.push(x);
}
// console.log(comand[0]);
}