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