250421 N과 M (2)

Jongleee·2025년 4월 21일
0

TIL

목록 보기
875/970
public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	StringTokenizer st = new StringTokenizer(br.readLine());

	int maxNumber = Integer.parseInt(st.nextToken());
	int selectCount = Integer.parseInt(st.nextToken());

	int[] combination = new int[selectCount];
	StringBuilder sb = new StringBuilder();

	generateCombinations(maxNumber, selectCount, 1, 0, combination, sb);
	System.out.print(sb);
}

private static void generateCombinations(int maxNumber, int selectCount, int start, int depth, int[] combination,
		StringBuilder sb) {
	if (depth == selectCount) {
		for (int num : combination) {
			sb.append(num).append(' ');
		}
		sb.append('\n');
		return;
	}

	for (int i = start; i <= maxNumber; i++) {
		combination[depth] = i;
		generateCombinations(maxNumber, selectCount, i + 1, depth + 1, combination, sb);
	}
}

출처:https://www.acmicpc.net/problem/15650

0개의 댓글