Combination(조합)
2025. 4. 8. 18:19
import java.util.Arrays;

public class Combination {
	
	static int N, R, totalCount;
	static int[] numbers = {3,5,7,8,9},ans;
	
	public static void main(String[] args) {
		N = numbers.length;
		R = 3;
		ans = new int[R];
		combination(0,0);
		System.out.println("===> "+totalCount);
	}

	private static void combination(int depth, int start) {
		if(depth == R) {
			totalCount++;
			System.out.println(Arrays.toString(ans));
			return;
		}
		for (int i = start; i < N; i++) {
			ans[depth] = numbers[i];
			combination(depth+1, i+1);//i+1 : for문 예제에서의 i+1, j+1의 의미를 갖음
		}
	}
}

'코딩테스트' 카테고리의 다른 글

백준 - 15649번 N과 M  (0) 2025.04.08
SubSet(부분집합)  (0) 2025.04.08
순열(Permutation) 예제  (0) 2025.04.08
순열(Permutation)  (0) 2025.04.08
백준 11659번 - 구간 합 구하기  (0) 2025.04.07