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

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

# Question

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

# Input

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

# Output

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

# Example Input

3 2
1

# Example Output

1
1

# Solution

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

1000๋ฒˆ A+B (opens new window) ๋ฌธ์ œ์™€ ๋™์ผํ•˜๊ฒŒ ์˜ˆ์ œ ์ž…๋ ฅ ๊ณผ์ •์—์„œ ๋ฐฐ์—ด ํ•จ์ˆ˜์ธ map ์„ ์‚ฌ์šฉํ•ด Number ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณค๋‹ค.

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

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