whatisthis?

๋ฐฑ์ค€ 15552 javascript (node.js) ํ’€์ด ๋ณธ๋ฌธ

ALGORITHM/BOJ (Node.js)

๋ฐฑ์ค€ 15552 javascript (node.js) ํ’€์ด

thisisyjin 2021. 12. 11. 21:42

for๋ฌธ - (4)

 

 


 

๐Ÿ’ก๋ฌธ์ œ

๋ณธ๊ฒฉ์ ์œผ๋กœ for๋ฌธ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์ „์— ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์ด ์žˆ๋‹ค. ์ž…์ถœ๋ ฅ ๋ฐฉ์‹์ด ๋Š๋ฆฌ๋ฉด ์—ฌ๋Ÿฌ ์ค„์„ ์ž…๋ ฅ๋ฐ›๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•  ๋•Œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค.

C++์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ณ  cin/cout์„ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, cin.tie(NULL)๊ณผ sync_with_stdio(false)๋ฅผ ๋‘˜ ๋‹ค ์ ์šฉํ•ด ์ฃผ๊ณ , endl ๋Œ€์‹  ๊ฐœํ–‰๋ฌธ์ž(\n)๋ฅผ ์“ฐ์ž. ๋‹จ, ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋” ์ด์ƒ scanf/printf/puts/getchar/putchar ๋“ฑ C์˜ ์ž…์ถœ๋ ฅ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋œ๋‹ค.

Java๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, Scanner์™€ System.out.println ๋Œ€์‹  BufferedReader์™€ BufferedWriter๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. BufferedWriter.flush๋Š” ๋งจ ๋งˆ์ง€๋ง‰์— ํ•œ ๋ฒˆ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

Python์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, input ๋Œ€์‹  sys.stdin.readline์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ, ์ด๋•Œ๋Š” ๋งจ ๋์˜ ๊ฐœํ–‰๋ฌธ์ž๊นŒ์ง€ ๊ฐ™์ด ์ž…๋ ฅ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ .rstrip()์„ ์ถ”๊ฐ€๋กœ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

๋˜ํ•œ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์€ ๋ณ„๊ฐœ์ด๋ฏ€๋กœ, ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ์ „๋ถ€ ์ž…๋ ฅ๋ฐ›์•„์„œ ์ €์žฅํ•œ ๋’ค ์ „๋ถ€ ์ถœ๋ ฅํ•  ํ•„์š”๋Š” ์—†๋‹ค. ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ํ•˜๋‚˜ ๋ฐ›์€ ๋’ค ํ•˜๋‚˜ ์ถœ๋ ฅํ•ด๋„ ๋œ๋‹ค.

์ž์„ธํ•œ ์„ค๋ช… ๋ฐ ๋‹ค๋ฅธ ์–ธ์–ด์˜ ๊ฒฝ์šฐ๋Š” ์ด ๊ธ€์— ์„ค๋ช…๋˜์–ด ์žˆ๋‹ค.

์ด ๋ธ”๋กœ๊ทธ ๊ธ€์—์„œ BOJ์˜ ๊ธฐํƒ€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํŒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ“์ž…๋ ฅ

์ฒซ ์ค„์— ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. T๋Š” ์ตœ๋Œ€ 1,000,000์ด๋‹ค. ๋‹ค์Œ T์ค„์—๋Š” ๊ฐ๊ฐ ๋‘ ์ •์ˆ˜ A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. A์™€ B๋Š” 1 ์ด์ƒ, 1,000 ์ดํ•˜์ด๋‹ค.

๐Ÿ“ˆ์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งˆ๋‹ค A+B๋ฅผ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ

5
1 1
12 34
5 500
40 60
1000 10003

์˜ˆ์ œ ์ถœ๋ ฅ

2
46
505
100
20006

 


< ์ฝ”๋“œ >

 

let fs = require("fs")
let input = fs.readFileSync("dev/stdin").toString().split("\n")

let answer = ''

for(let i = 1; i <= input[0]; i++) {
    tmp = input[i].split(' ')
    answer += parseInt(tmp[0]) + parseInt(tmp[1]) + "\n"
}

console.log(answer)

for๋ฌธ ์•ˆ์— console.log๋ฅผ ์ž…๋ ฅํ•˜์ง€ ๋ง๊ณ 

answer ๋ณ€์ˆ˜ (string)๋ฅผ ๊ณต๋ฐฑ('')์œผ๋กœ ์„ ์–ธํ•œ ํ›„ +์—ฐ์‚ฐ์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋”ํ•œ๋‹ค.

์ค„๋ฐ”๊ฟˆ์€ '\n' (๊ฐœํ–‰๋ฌธ์ž) ๋กœ ํ•˜๊ณ , ๋‹ค ๋”ํ•œ answer์„ ๋งˆ์ง€๋ง‰์— ์ถœ๋ ฅํ•˜์—ฌ ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ๋ฐฉ์ง€.

 

 

 


 

 

 

fs๊ฐ€ ์•„๋‹Œ readline์œผ๋กœ ํ•œ๋‹ค๋ฉด? ๐Ÿ”ป

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

let answer = '';
rl.on('line', line => {
  const input = line.split(' ');
    
  if(input.length === 2) {
    const A = parseInt(input[0]);
    const B = parseInt(input[1]);
    answer += A+B + '\n';
  }
}).on('close', () => {
  console.log(answer);
  process.exit();
})

 

 

 

์ฒ˜์Œ์— ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ ๐Ÿ”ป

 

let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
let testCase = Number(input[0]);

for(let i=1; i<=testCase; i++) {
   let numArr = input[1].split(' ');
   console.log(Number(numArr[0]) + Number(numArr[0]));
}