다른 알고리즘 관련 사이트와 달리 BOJ는 입출력 구현부터 해줘야 한다.
문제풀이 할 때 참고하기 위해 포스팅을 적어본다.
BOJ 입출력 정리 참고 사이트
<node.js 내부 모듈 - fs 모듈>
const fs = require("fs");
◈ 문자 하나인 경우
const input = fs.readFileSync("/dev/stdin").toString();
◈ 한 칸 띄어쓰기로 구분할 경우 input[0], input[1] 배열을 이용
const input = fs.readeFileSync("/dev/stdin").toString().split(" ");
◈ 줄바꿈으로 구분
const input = fs.readFileSync("/dev/stdin").toString().split("\n");
◈ 입력값이 숫자인 경우
const input = fs.readFileSync("/dev/stdin").toString().split(" ").map(function(a) {
return + a
});
🔽 Window 로컬에서 BOJ 예제 테스트하기 참고 사이트
// 10950 A+B - 3 예시
const fs = require('fs');
const stdin = (process.platform === 'linux'
? fs.readFileSync('/dev/stdin').toString()
: `5
1 1
2 3
3 4
9 8
5 2
`
).split('\n');
const input = (() => {
let line = 0;
return () => stdin[line++];
})();
let t = input();
while (t--) {
const [a, b] = input().split(' ').map(Number);
console.log(a + b);
}
➕ 코드는 잘 작성했으나 '시간 초과'되어 통과되지 못한다면?
console.log는 속도가 느리기 때문에 정렬된 배열의 요소 하나씩 호출하면 최대 100만번 호출된다.
정렬 후 결과값 출력 시에는 배열 join을 써서 하나의 문자열로 출력하면 통과된다.