프로그래머스 문제풀이에만 익숙해져 있어서 엄청 했던 당황했던 백준 문제풀이
입출력 문제에서 "출력"은 익숙하지만 "입력"은 나에게 어색한 존재
A+B가 답인데도 못 풀다니.. 적잖은 충격..
친절하게 나와있는 백준 언어 도움말의 도움을 받아서 풀었다.
// node.js
var fs = require('fs');
var input = fs.readFileSync('/dev/stdin').toString().split(' ');
var a = parseInt(input[0]);
var b = parseInt(input[1]);
console.log(a+b);
var fs = require('fs');
var input = fs.readFileSync('/dev/stdin').toString().split(' ');
require('fs')
FileSystem의 약자인 fs 모듈은 파일을 처리하는 모듈로 직접 입력 파일을 읽어와서 처리
readFileSync('/dev/stdin')
표준 입력값을 받아서 처리
toString()
require('fs').readFileSync('/dev/stdin')의 반환값은 문자열이 아닌 Buffer 객체
readFileSync의 인수로 인코딩을 지정해주지 않으면 Buffer 객체를 반환
따라서 문자열로 바꾸어주지 않으면 예기치 못한 오류가 남
그렇기 때문에 toString()을 통하여 문자열로 반환
split(' ')
입력값을 2개 받기 위해 split 메서드를 통해 문자 배열로 반환
그 외의 값 입력받는 법
1-1. 하나의 값을 입력 받을 때 (문자)
const fs = require('fs');
const input = fs.readFileSync("/dev/stdin").toString().trim();
/*
input: hello
output: hello
*/
1-2. 하나의 값을 입력 받을 때 (숫자)
const fs = require('fs');
const input = fs.readFileSync("/dev/stdin").toString().trim();
const number = +input; // or parseInt(input);
/*
input: 5
output: 5
*/
2-1. 공백으로 구분된 한 줄의 값들을 입력 받을 때 (문자)
const fs = require('fs');
const input = fs.readFileSync("/dev/stdin").toString().trim().split(" ");
/*
input: hello world
output: ['hello', 'world']
*/
2-2. 공백으로 구분된 한 줄의 값들을 입력 받을 때 (숫자)
const fs = require('fs');
const input = fs.readFileSync("/dev/stdin").toString().trim().split(" ").map(Number);
/*
input: 95 6 12
output: [95, 6, 12]
*/
3-1. 여러 줄의 값들을 입력 받을 때 (문자)
const fs = require('fs');
const input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
/*
input:
a
b
c
d
output: ['a', 'b', 'c', 'd']
*/
3-2. 여러 줄의 값들을 입력 받을 때 (숫자)
const fs = require('fs');
const input = fs.readFileSync("/dev/stdin").toString().trim().split("\n").map(Number);
/*
input:
1
2
3
4
output: [1, 2, 3, 4]
*/
4-1. 공백으로 구분된 여러줄의 값들이 띄어쓰기로 구분되어 있을 때 (문자)
const fs = require('fs');
const input = fs.readFileSync("/dev/stdin").toString().trim().split("\n")
.map(el => el(split(" ")));
/*
input:
hello world
hi there
good bye
output: [
['hello', 'world'],
['hi'], ['there'],
['good', 'bye']
]
*/
4-2. 공백으로 구분된 여러줄의 값들이 띄어쓰기로 구분되어 있을 때 (숫자)
const fs = require('fs');
const input = fs.readFileSync("/dev/stdin").toString().trim().split("\n")
.map(el => el(split(" ").map(Number)));
/*
input:
1 2
3 4 5
12 34 56
output: [
[1, 2],
[3, 4, 5],
[12, 34, 56]
]
*/
5. 첫 번째 줄에서 자연수 n을 입력받고, 그 다음줄에 공백으로 구분된 n개의 값들을 입력 받을 때
const fs = require('fs');
const [n, ...arr] = fs.readFileSync('/dev/stdin').toString().trim().split(/\s/);
6. 첫 번째 줄에서 자연수 n을 입력받고, 그 다음줄 부터 n개의 줄에 걸쳐 한줄에 하나의 값을 입력 받을 때
const fs = require('fs');
const [n, ...arr] = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
7. 첫 번째 줄에 자연수 n을 입력받고, 그 다음줄 부터 n개의 줄에 걸쳐 한 줄에 하나의 값을 입력 받을 때
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split(/\s/);
참조
https://www.daleseo.com/js-node-fs/
Node.js의 fs 모듈로 파일 입출력 처리하기
Engineering Blog by Dale Seo
www.daleseo.com
https://tesseractjh.tistory.com/39
Node.js로 백준(BOJ) 문제 풀 때 유의할 점들
백준에서 Node.js로 입력을 받는 방법은 크게 두 가지가 있다. 첫 번째는 readline 모듈을 사용하는 것이고, 두 번째는 fs 모듈을 사용하는 것이다. (이 글에서는 fs 모듈에 대해서만 다루겠다.) Python으
tesseractjh.tistory.com
'ETC > 📋 Coding Tests' 카테고리의 다른 글
[백준/node.js] 15552번 빠른 A+B :: 시간초과 (0) | 2023.11.29 |
---|---|
[백준/node.js] fs 모듈 런타임 에러 (0) | 2023.11.28 |
주니어 개발자의 코알못 탈출기 🏃🏻♂️ (2) | 2023.11.23 |
[프로그래머스] 추억 점수 (JS) - Level.1 (0) | 2023.11.11 |
[프로그래머스] 배열 두 배 만들기 (0) | 2023.04.03 |