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