CS/Algorithm
(알고리즘) N과 M (4) - 중복조합
주누
2020. 10. 21. 01:31
15652번: N과 M (4)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
문제 설명
조합은 순서에 대한 구분이 따로 없으며 이 조건에서 앞의 문제와 같이 중복을 허용하면 된다.
이를 위해서 기존 조합 문제와는 달리 i에 +1을 하지 않는다.
소스 코드
public class Problem_15652 {
private static int n;
private static int m;
private static int[] arr;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
m = scanner.nextInt();
arr = new int[m];
dfs(1, 0);
}
private static void dfs(int index, int depth) {
if (depth == m) {
for (int i = 0; i < m; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
return;
}
for (int i = index; i <= n; i++) {
arr[depth] = i;
dfs(i, depth + 1);
}
}
}
Code Link
mike6321/Algorithm
알고리즘 공부를 위한 repository 입니다. Contribute to mike6321/Algorithm development by creating an account on GitHub.
github.com