whatisthis?

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

ALGORITHM/BOJ (Node.js)

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

thisisyjin 2021. 12. 13. 21:22

while๋ฌธ - (1)

 

 


 

๐Ÿ’ก๋ฌธ์ œ

๋‘ ์ •์ˆ˜ A์™€ B๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋‹ค์Œ, A+B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๐Ÿ“์ž…๋ ฅ

์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ฐ ์ค„์— A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (0 < A, B < 10)

์ž…๋ ฅ์˜ ๋งˆ์ง€๋ง‰์—๋Š” 0 ๋‘ ๊ฐœ๊ฐ€ ๋“ค์–ด์˜จ๋‹ค.

๐Ÿ“ˆ์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค A+B๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ

1 1
2 3
3 4
9 8
5 2
0 0

์˜ˆ์ œ ์ถœ๋ ฅ

2
5
7
17
7

 


< ์ฝ”๋“œ >

 

let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
let i = 0;
let ans = '';

while(input[i] != '0 0') {
   let nums = input[i].split(' ');
   ans += Number(nums[0]) + Number(nums[1]) + '\n';
   i += 1;
}
console.log(ans);

์ค„๋งˆ๋‹ค ๋‚˜๋ˆ ์„œ ๋ฐฐ์—ด ์•„์ดํ…œ์œผ๋กœ ์ €์žฅํ•˜๋Š” input๋ฐฐ์—ด์„ ์šฐ์„  ์„ ์–ธํ•œ๋‹ค.

i = 0์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•œ ํ›„

 

while(input[i] != '0 0')

// ๋‚ด์šฉ

i += 1;

๊ณผ ๊ฐ™์ด ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•œ๋‹ค.

 

 

๋งค ์ค„๋งˆ๋‹ค input[i]๋ฅผ ๋‚˜๋ˆ ์•ผํ•˜๋ฏ€๋กœ while๋ฌธ ์•ˆ์—์„œ i๊ฐ€ ์ฆ๊ฐ€ํ•  ๋•Œ ๋งˆ๋‹ค split ํ•ด์ค€๋‹ค.

๋‚˜๋ˆˆ ๋ฐฐ์—ด์„ nums ๋ผ๊ณ  ํ•˜๊ณ , nums[0] + nums[1]์„ ํ•ด์ค€๋‹ค. ** ์ฃผ์˜ - Number()์•ˆ์— ๋„ฃ์–ด์คŒ

 

 

 

 

 

 

 

โŒ ์ฒซ ์ œ์ถœ - ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ (SyntaxError)

let input = require('fs').readFileSync(/dev/stdin).toString().split('\n');
let i = 0;
let ans = '';

while(input[i] != '0 0') {
   let nums = input[i].split(' ');
   ans += Number(nums[0]) + Number(nums[1]) + '\n';
   i += 1;
}
console.log(ans);

- readFileSync(/dev/stdin) ์—์„œ readFileSync('/dev/stdin')๋กœ ๋ณ€๊ฒฝํ•ด์•ผํ•จ -> ๊ตฌ๋ฌธ์˜ค๋ฅ˜