CS/Algorithm
(알고리즘) N과 M (2) - 조합
주누
2020. 10. 21. 01:00
15650번: N과 M (2)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
문제 설명
이 문제는 이전에 앞전에 포스팅하였던 내용 중 순서가 없는 조합에 대한 문제이다.
조합에 대한 문제는 따로 방문노드를 설정할 필요 없이 depth와 인덱스 값만 들고 다니면 된다.
(알고리즘) 순열과 조합의 차이
순열과 조합의 차이는 무엇인가? 순열을 알아보기 전에 순열과 조합의 차이를 먼저 알아보자! 순열과 조합의 가장 큰 차이는 순서가 있고 없고 이다. 예를 들어 자전거의 번호가 있는 자물쇠를 �
jwdeveloper.tistory.com
소스코드
public class Problem_15650 {
private static int n;
private static int m;
private static int[] arr;
private static boolean[] visited;
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 (m == depth) {
for (int value : arr) {
System.out.print(value + " ");
}
System.out.println();
return;
}
for (int i = index; i <= n; i++) {
arr[depth] = i;
dfs(i + 1, depth + 1);
}
}
}
Code Link
mike6321/Algorithm
알고리즘 공부를 위한 repository 입니다. Contribute to mike6321/Algorithm development by creating an account on GitHub.
github.com