๐ป์ ๋ ฅ๊ฐ์ ํ์ธํ๊ธฐ ์ํด ์ถ๋ ฅ์ ํด๋ณด์๋๋ ์ถ๋ ฅ๊ฐ์ด ์ด์ํ๋ค
function main () {
// let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
const input = require('fs').readFileSync('txt/10828.txt').toString().split('\n');
const test = +input[0];
for (let i = 1; i <= test; i++) {
const command = input[i].split(' ').map(item => !isNaN(item) ? parseInt(item) : item);
console.log(command);
}
}
main();
๐ป์ถ๋ ฅ๊ฐ
[ 'push', 1 ]
[ 'push', 2 ]
[ 'top\r' ]
[ 'size\r' ]
[ 'empty\r' ]
[ 'pop\r' ]
[ 'pop\r' ]
[ 'pop\r' ]
[ 'size\r' ]
[ 'empty\r' ]
[ 'pop\r' ]
[ 'push', 3 ]
[ 'empty\r' ]
[ 'top' ]
\r
์บ๋ฆฌ์ง ๋ฆฌํด์ ๋ํ๋ด๋ ์ด์ค์ผ์ดํ ์ํ์ค
readFileSync ํจ์๋ก ํ์ผ์ ์ฝ์ด์ค๋ ๊ฒฝ์ฐ, Windows ์ด์์ฒด์ ์์ ์ค ๋ฐ๊ฟ์ ๋ํ๋ด๋ ๊ธฐํธ
\r\n
(์บ๋ฆฌ์ง ๋ฆฌํด + ๋ผ์ธ ํผ๋)์ ์ฌ์ฉ
๐ input ๋ฐ์ ๋split('\r\n')
์ผ๋ก ๋์ด์ค๋ค.
function main () {
// let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
const input = require('fs').readFileSync('txt/10828.txt').toString().split('\r\n');
const test = +input[0];
for (let i = 1; i <= test; i++) {
const command = input[i].split(' ').map(item => !isNaN(item) ? parseInt(item) : item);
console.log(command);
}
}
main();
๐ป์ถ๋ ฅ๊ฐ
[ 'push', 1 ]
[ 'push', 2 ]
[ 'top' ]
[ 'size' ]
[ 'empty' ]
[ 'pop' ]
[ 'pop' ]
[ 'pop' ]
[ 'size' ]
[ 'empty' ]
[ 'pop' ]
[ 'push', 3 ]
[ 'empty' ]
[ 'top' ]
function stack (command, num) {
let stack = [];
if (command === 'push') {
const number = parseInt(num);
stack.push(number);
return stack;
} else if (command === 'top') {
return stack.length === 0? -1 : stack[stack.length-1];
} else if (command === 'size') {
return stack.length;
} else if (command === 'empty') {
return stack.length === 0 ? 1 : 0;
} else if (command === 'pop') {
return stack.length === 0? -1 : stack.pop();
}
}
function main () {
// const input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
const input = require('fs').readFileSync('txt/10828.txt').toString().split('\r\n');
const test = +input[0];
for (let i = 1; i <= test; i++) {
const [command, num] = input[i].split(' ');
const result = stack(command, num);
console.log(i, command, num, result);
}
}
main();
์๊ฐ์ด๊ณผ
function main () {
// const input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
const input = require('fs').readFileSync('txt/10828.txt').toString().split('\r\n');
const test = +input[0];
let stack = [];
for (let i = 1; i <= test; i++) {
const [command, num] = input[i].split(' ');
switch(command) {
case 'push' :
const number = parseInt(num);
stack.push(number);
break;
case 'top' :
console.log(stack.length === 0? -1 : stack[stack.length-1]);
break;
case 'size' :
console.log(stack.length);
break;
case 'empty' :
console.log(stack.length === 0 ? 1 : 0);
break;
case 'pop' :
console.log(stack.length === 0? -1 : stack.pop());
break;
}
}
}
main();
์๊ฐ์ด๊ณผ
console.log() ๋ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆฐ๋ค.
ํ๋ฒ์ ๋ชจ์์ ์ถ๋ ฅ์ ํ๋ค.
์๋ก์ด ๋ฐฐ์ด์ ์ ์ธํ๊ณ ๊ทธ ๋ฐฐ์ด์ ๊ฒฐ๊ณผ๊ฐ๋ค์ pushํ ํ
๋ง์ง๋ง์ join("\n")์ ์ฌ์ฉํ์ฌ ์ถ๋ ฅํ๋ค.
if๋ฌธ์ ํ๋ ํ๋ ๋๋ฉด ์๊ฐ์ด ๋ ์ค๋ ๊ฑธ๋ฆด๊ฒ๊ฐ์ function์ผ๋ก ๋ง๋ ใ
ํ๋ฒ๋น ๋ชจ์ ์ถ๋ ฅํ๋ค.
function funcPush(stack, num) {
const number = parseInt(num);
stack.push(number);
}
function funcTop (stack) {
return stack.length === 0? -1 : stack[stack.length-1];
}
function funcSize (stack) {
return stack.length;
}
function funcEmpty (stack) {
return stack.length === 0 ? 1 : 0;
}
function funcPop (stack) {
return stack.length === 0? -1 : stack.pop();
}
function main () {
// const input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
const input = require('fs').readFileSync('txt/10828.txt').toString().split('\r\n');
const test = +input[0];
let stack = [];
const result = [];
for (let i = 1; i <= test; i++) {
const [command, num] = input[i].split(' ');
switch(command) {
case 'push' :
funcPush(stack, num)
break;
case 'top' :
result.push(funcTop(stack))
break;
case 'size' :
result.push(funcSize(stack))
break;
case 'empty' :
result.push(funcEmpty(stack))
break;
case 'pop' :
result.push(funcPop(stack))
break;
}
}
console.log(result.join("\n"))
}
main();