File tree Expand file tree Collapse file tree 2 files changed +91
-0
lines changed
백준/Silver/15664. N과 M (10) Expand file tree Collapse file tree 2 files changed +91
-0
lines changed 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+ combination (0 , 0 , N , R );
27+ System .out .println (sb .toString ());
28+ }
29+
30+ private static void combination (int start , 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 = start ; i < N ; i ++) {
41+ if (arr [i ] == prev ) {
42+ continue ;
43+ }
44+
45+ prev = arr [i ];
46+ output [depth ] = arr [i ];
47+ combination (i + 1 , depth + 1 , N , R );
48+ }
49+ }
50+ }
Original file line number Diff line number Diff line change 1+ # [ Silver II] N과 M (10) - 15664
2+
3+ [ 문제 링크] ( https://www.acmicpc.net/problem/15664 )
4+
5+ ### 성능 요약
6+
7+ 메모리: 17752 KB, 시간: 164 ms
8+
9+ ### 분류
10+
11+ 백트래킹
12+
13+ ### 제출 일자
14+
15+ 2026년 2월 19일 17:07:46
16+
17+ ### 문제 설명
18+
19+ <p >N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오.</p >
20+
21+ <ul >
22+ <li>N개의 자연수 중에서 M개를 고른 수열</li>
23+ <li>고른 수열은 비내림차순이어야 한다.
24+ <ul>
25+ <li>길이가 K인 수열 A가 A<sub>1</sub> ≤ A<sub>2</sub> ≤ ... ≤ A<sub>K-1</sub> ≤ A<sub>K</sub>를 만족하면, 비내림차순이라고 한다.</li>
26+ </ul>
27+ </li>
28+ </ul >
29+
30+ ### 입력
31+
32+ <p >첫째 줄에 N과 M이 주어진다. (1 ≤ M ≤ N ≤ 8)</p >
33+
34+ <p >둘째 줄에 N개의 수가 주어진다. 입력으로 주어지는 수는 10,000보다 작거나 같은 자연수이다.</p >
35+
36+ ### 출력
37+
38+ <p >한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다.</p >
39+
40+ <p >수열은 사전 순으로 증가하는 순서로 출력해야 한다.</p >
41+
You can’t perform that action at this time.
0 commit comments