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
