본문 바로가기
알고리즘/백준

백준 nodejs 기준 기본코드 실행소스

by 데브믹서 2022. 8. 3.

백준에서 알고리즘 문제를 풀때

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번 문제로
주어진 두개의 값을 각각 순서를 역순서로 바꾼뒤
큰 값을 리턴하게 하는 기능이다. 


ㄴ 위의 코드를 넣고 실행한 결과