# [Algorithm/JS] ๋ฐฑ์ค€ 15649๋ฒˆ N๊ณผ M (1)

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

# Question

์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์•„๋ž˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ธธ์ด๊ฐ€ M์ธ ์ˆ˜์—ด์„ ๋ชจ๋‘ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

  • 1๋ถ€ํ„ฐ N๊นŒ์ง€ย ์ž์—ฐ์ˆ˜ ์ค‘์—์„œ ์ค‘๋ณต ์—†์ด M๊ฐœ๋ฅผ ๊ณ ๋ฅธ ์ˆ˜์—ด

# Input

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค M โ‰ค N โ‰ค 8)

# Output

ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ๋˜๋ฉฐ, ๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

# Example

# Input 1

3 1
1

# Output 1

1
2
3
1
2
3

# Input 2

4 2
1

# Output 2

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

# Input 3

4 4
1

# Output 3

1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# Solution

# Solution

const fs = require('fs');
const [N, M] = fs.readFileSync('dev/stdin').toString().trim().split(' ').map(Number);
const check = new Array(9).fill(false);
const arr = [];
function sequnce(cnt) {
  if (cnt === M) {
    console.log(arr.join(' '));
    return;
  }
  for (let i = 1; i <= N; i++) {
    if (!check[i]) {
      check[i] = true;
      arr[cnt] = i;
      sequnce(cnt + 1);
      check[i] = false;
    }
  }
}
sequnce(0);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ํ’€์ด

์ฐธ๊ณ  https://nyang-in.tistory.com/m/284 (opens new window)

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