JavaScript로 백준 문제 풀기(제출하기)

minkyeongJ·2022년 5월 8일
2

백준은 JavaScript로 제출하는 조건이 없다. 그래서 node.js를 이용하여 문제를 제출해야한다.

백준 저지에 있는 node.js 가이드

// 파일을 읽어오기 위해 Node.js의 built-in file system module fs 사용
var fs = require('fs'); 

//input을 읽어와 변수로 선언 & 할당
// 그 내용을 input에 저장, toString(), split()을 사용해서
// Array로 저장된다.
var input = fs.readFileSync('/dev/stdin').toString().split(' ');

// input에서 지정한 value를 읽어와 다른 변수로 선언하고 활용
// toString메소드로 지금은 string이기 때문에, parseInt로 숫자로 형태변환을 해준다.
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(' ');
var a = parseInt(input[0]);
var b = parseInt(input[1]);
console.log(a + b)
const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let input = fs.readFileSync(filePath).toString().split('\n');
input = input.toString().split(' ').map((item) => +item);
const fs = require('fs')
const inputData = fs.readFileSync('/dev/stdin').toString().split(' ').map(value =>
                                                                         +value)

const [a, b] = inputData

참고

profile
멋진 프론트엔드 개발자를 위하여!

0개의 댓글