File tree Expand file tree Collapse file tree
백준/Silver/15665. N과 M (11) Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .Arrays ;
2+ import java .util .Scanner ;
3+
4+ public class Main {
5+
6+ static int [] arr ;
7+ static int [] output ;
8+ static StringBuilder sb = new StringBuilder ();
9+
10+ public static void main (String [] args ) {
11+ Scanner sc = new Scanner (System .in );
12+ String [] input1 = sc .nextLine ().split (" " );
13+ int N = Integer .parseInt (input1 [0 ]);
14+ int R = Integer .parseInt (input1 [1 ]);
15+
16+ String [] input2 = sc .nextLine ().split (" " );
17+
18+ output = new int [R ];
19+ arr = new int [N ];
20+
21+ for (int i = 0 ; i < arr .length ; i ++) {
22+ arr [i ] = Integer .parseInt (input2 [i ]);
23+ }
24+
25+ Arrays .sort (arr );
26+ repeatPermutation (0 , N , R );
27+ System .out .println (sb .toString ());
28+ }
29+
30+ private static void repeatPermutation (int depth , int N , int R ) {
31+ if (depth == R ) {
32+ for (int i = 0 ; i < R ; i ++) {
33+ sb .append (output [i ]).append (' ' );
34+ }
35+ sb .append ('\n' );
36+ return ;
37+ }
38+
39+ int prev = Integer .MIN_VALUE ;
40+ for (int i = 0 ; i < N ; i ++) {
41+ if (arr [i ] == prev ) {
42+ continue ;
43+ }
44+
45+ prev = arr [i ];
46+ output [depth ] = arr [i ];
47+ repeatPermutation (depth + 1 , N , R );
48+ }
49+ }
50+ }
Original file line number Diff line number Diff line change 1+ # [ Silver II] N과 M (11) - 15665
2+
3+ [ 문제 링크] ( https://www.acmicpc.net/problem/15665 )
4+
5+ ### 성능 요약
6+
7+ 메모리: 35940 KB, 시간: 396 ms
8+
9+ ### 분류
10+
11+ 백트래킹
12+
13+ ### 제출 일자
14+
15+ 2026년 2월 19일 23:06:34
16+
17+ ### 문제 설명
18+
19+ <p >N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오.</p >
20+
21+ <ul >
22+ <li>N개의 자연수 중에서 M개를 고른 수열</li>
23+ <li>같은 수를 여러 번 골라도 된다.</li>
24+ </ul >
25+
26+ ### 입력
27+
28+ <p >첫째 줄에 N과 M이 주어진다. (1 ≤ M ≤ N ≤ 7)</p >
29+
30+ <p >둘째 줄에 N개의 수가 주어진다. 입력으로 주어지는 수는 10,000보다 작거나 같은 자연수이다.</p >
31+
32+ ### 출력
33+
34+ <p >한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다.</p >
35+
36+ <p >수열은 사전 순으로 증가하는 순서로 출력해야 한다.</p >
37+
You can’t perform that action at this time.
0 commit comments