1부터 N까지 번호가 적힌 구슬들 중 중복을 허락하여 M번을 뽑아 일렬로 나열하는 방법을 모두 출력하는 프로그램
import java.util.Scanner;
public class Main {
static int n, m;
static int[] arr;
public static void DFS(int level) {
if (level==m) {
for (int x : arr) {
System.out.print(x + " ");
}
System.out.println();
}
else {
for (int i=1; i<=n; i++) {
arr[level] = i;
DFS(level+1);
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt(); // 구슬 개수
m = scanner.nextInt(); // 뽑는 개수
arr = new int[m]; // 중복순열
DFS(0);
}
}
3 2
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
댓글