# [Algorithm/JS] ๋ฐฑ์ค€ 1000๋ฒˆ A+B

๐Ÿ”— ๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ (opens new window)

# Question

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

# Input

์ฒซ์งธ ์ค„ A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค (0 < A, B < 10)

# Output

์ฒซ์งธ ์ค„์— A+B๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

# Example Input

1 2
1

# Example Output

3
1

# Solution 1

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().split('').map(Number);

let answer = input.reduce((a, b) => {
  return a + b;
});

console.log(answer);
1
2
3
4
5
6
7
8

์˜ˆ์ œ ์ž…๋ ฅ ๊ณผ์ •์—์„œ ๋ฐฐ์—ด ํ•จ์ˆ˜์ธ map ์„ ์‚ฌ์šฉํ•ด Number ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณค๋‹ค.

reduce ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์„ ํ•œ๊ฐœ์˜ ๊ฐ’์œผ๋กœ ์ค„์—ฌ์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋ฉฐ, ๋ณดํ†ต ์ดํ•ฉ, ํ‰๊ท , ์ตœ์†Œ, ์ตœ๋Œ€ ๊ฐ’์„ ๊ตฌํ•  ๋•Œ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— answer ๋ณ€์ˆ˜์— reduce๋ฅผ ์‚ฌ์šฉํ•ด ํ•ด๋‹น ๋ฐฐ์—ด์˜ ์ด ํ•ฉ์„ ๊ตฌํ–ˆ๋‹ค.

# ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค ํ’€์ด

# Soultion 1

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);
1
2
3
4
5
6
7

parseInt๋กœ ์ •์ˆ˜ํ˜• ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•ด ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ง์ ‘ ์ง€์ •ํ•˜๊ณ , ๊ฐ’์„ ๋ฐ›์•„์™€ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

# Soultion 2

const fs = require('fs');
const input = fs
  .readFileSync('/dev/stdin')
  .toString()
  .split(' ')
  .map((value) => +value);
const [a, b] = input;
console.log(a + b);
1
2
3
4
5
6
7
8

๋ฐ›์•„์˜จ ์˜ˆ์ œ๋ฅผ number ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๊ณผ์ •์„ map ์œผ๋กœ ํ†ตํ•ด ๊ตฌํ˜„ํ–ˆ์œผ๋ฉฐ ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น์œผ๋กœ a ์™€ b๋ฅผ ๋ฐ›์•„ ํ•ฉ์„ ์ถœ๋ ฅํ–ˆ๋‹ค.

Last Updated: 2022. 6. 5. ์˜คํ›„ 3:42:39