# [Algorithm/JS] ๋ฐฑ์ค€ 3052๋ฒˆ ๋‚˜๋จธ์ง€

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

# Question

๋‘ ์ž์—ฐ์ˆ˜ A์™€ B๊ฐ€ ์žˆ์„ ๋•Œ, A%B๋Š” A๋ฅผ B๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 7, 14, 27, 38์„ 3์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋Š” 1, 2, 0, 2์ด๋‹ค.

์ˆ˜ 10๊ฐœ๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋’ค, ์ด๋ฅผ 42๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•œ๋‹ค. ๊ทธ ๋‹ค์Œ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์ด ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

# Input

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ ์—ด๋ฒˆ์งธ ์ค„ ๊นŒ์ง€ ์ˆซ์ž๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ด ์ˆซ์ž๋Š” 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๊ณ , ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜์ด๋‹ค.

# Output

์ฒซ์งธ ์ค„์—, 42๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ, ์„œ๋กœ ๋‹ค๋ฅธ ๋‚˜๋จธ์ง€๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

# Example Input 1

1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10

# Example Output 1

10
1

# Example Input 2

42
84
252
420
840
126
42
84
420
126
1
2
3
4
5
6
7
8
9
10

# Example Output 2

1
1

# Example Input 2

39
40
41
42
43
44
82
83
84
85
1
2
3
4
5
6
7
8
9
10

# Example Output 2

6
1

# Solution

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ํ’€์ด๋Š” ์ด์™€ ๊ฐ™๋‹ค.

const fs = require('fs');
const input = fs.readFileSync('dev/stdin').toString().split('\n').map(Number);
const arr = [];

input.map((n) => arr.push(n % 42));
console.log([...new Set(arr)].length);
1
2
3
4
5
6

๊ฒฐ๊ณผ๋Š” ํ‹€๋ ธ๋‹ค๊ณ  ํ•œ๋‹ค. ์•„๋ฌด๋ฆฌ ๋ด๋„ ํ‹€๋ฆฐ ์ ์ด ์—†๊ณ , ์˜ˆ์ œ์™€ ๋™์ผํ•œ Output์ด ๋‚˜์˜ค๋Š”๋ฐ ์–ด๋””์„œ ํ‹€๋ฆฐ ๊ฒƒ์ผ๊นŒ? ์งˆ๋ฌธ ๊ฒŒ์‹œํŒ์„ ๋’ค์ ธ๋ณด์•˜๊ณ ,

3052๋ฒˆ nodejs ๋ฐ˜๋ ˆ๋ฅผ ๋ชป์ฐพ๊ฒ ์Šต๋‹ˆ๋‹ค. (opens new window)

์ด ๊ธ€์„ ๋ณด๊ณ  ํ•ด๋‹ต์„ ์ฐพ์•˜๋‹ค. Input ์„ ๋ฐ›์•„์˜ฌ ๋•Œ split() ์ด์ „์— trim()์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค! ์ž…๋ ฅ๊ฐ’์— ๊ณต๋ฐฑ์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

๊ณต๋ฐฑ์ด ์–ด๋””์— ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

const input = fs.readFileSync('dev/stdin').toString().split('\n').map(Number);
console.log(`๊ณต๋ฐฑ${input}์–ด๋””์—`); // ๊ณต๋ฐฑ39,40,41,42,43,44,82,83,84,85์–ด๋””์—
1
2

๊ณต๋ฐฑ.. ์–ด๋””์— ์žˆ์„๊นŒ? ๋‚˜๋Š” ๋ชจ๋ฅด๊ฒ ๋‹ค. ๐Ÿค”

# ์ •๋‹ต ํ’€์ด

๋‹ค์‹œ ์ œ์ถœํ•œ ํ’€์ด๋Š” ์ด์™€ ๊ฐ™๋‹ค.

const fs = require('fs');
const input = fs.readFileSync('dev/stdin').toString().trim().split('\n').map(Number);
const arr = [];

input.map((n) => arr.push(n % 42));
console.log([...new Set(arr)].length);
1
2
3
4
5
6

Set ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด arr ์˜ ์ค‘๋ณต๊ฐ’์„ ์ œ๊ฑฐํ–ˆ๋‹ค. [...new Set(arr)]๋Œ€์‹  Array.from(new Set(arr))๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

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