백준에서 알고리즘 문제를 풀때
nodejs 를 이용할때 사용되는 소스코드이다.
제출용 + 로컬 (리눅스) 양쪽에서 둘다 동작한다
const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
let input = fs.readFileSync(filePath).toString().split("\n");
//console.log(input);
input = input[0];
//console.log(input);
input = input.split(" ").map((item) => +item);
//console.log(input);
exe(input[0], input[1]);
function exe(A, B) {
let a1 = reverse(A);
let b1 = reverse(B);
if(a1 > b1)
console.log(a1);
else
console.log(b1);
}
function reverse(str)
{
str = str+'';
return str.split('').reverse().join('');
}
로컬에서 테스트 할때는 input.txt 에 테스트 데이터를 집어넣고 사용한다
사용방법은
$ node work.js < input.txt
이렇게 콘솔에서 이용이 가능하다
위의 소스코드는 백준 2908번 문제로
주어진 두개의 값을 각각 순서를 역순서로 바꾼뒤
큰 값을 리턴하게 하는 기능이다.