알고리즘/백준
[백준 자바스크립트] node.js 환경에서 콘솔 입출력
gyujh
2023. 6. 2. 19:16
js로 백준 문제를 풀려고 봤더니 백준에서는 node.js 환경으로 코드를 제출해야 했다.
여기에 입출력 관련한 코드를 정리할 예정이다.
node.js 콘솔 실행방법
터미널 Command 입력 -> node 파일이름.js
입출력 코드
// 1. 한줄, 공백 x, 하나만 입력
// ex) "hello" -> "hello"
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin
});
rl.question('', (input) => {
void answer(input);
rl.close();
});
//답안 작성부분
function answer(input) {
console.log(input);
}
// 2. 한줄 입력 + 공백 구분
// ex) "hello world" -> ['hello', 'world']
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin
});
rl.question('', (input) => {
input = input.trim().split(' ');
void answer(input);
rl.close();
});
//답안 작성부분
function answer(input) {
console.log(input);
}
// 3. 한줄 입력 + 공백 구분 + 숫자 변환
// ex) "3 40" -> [3, 40]
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin
});
rl.question('', (input) => {
input = input.trim().split(' ');
input = input.map(item => parseInt(item));
void answer(input);
rl.close();
});
//답안 작성부분
function answer(input) {
console.log(input);
}