-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combinatorics8.java
57 lines (51 loc) · 1.54 KB
/
Combinatorics8.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package ru.itmo.chizhikov;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Combinatorics8 {
static int[] nextCombination(int[] a, int n, int k) {
int b[] = new int[n];
for (int i = 0; i < k; i++) {
b[i] = a[i];
}
b[k] = n + 1;
int ind = k - 1;
while (ind >= 0 && b[ind + 1] - b[ind] < 2) {
ind--;
}
if (ind >= 0) {
b[ind]++;
for (int j = ind + 1; j < k; j++) {
b[j] = b[j - 1] + 1;
}
for (int i = 0; i < k; i++) {
a[i] = b[i];
}
return a;
} else return null;
}
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(new File("choose.in"));
FileWriter fileWriter = new FileWriter(new File("choose.out"));
int n = scanner.nextInt();
int k = scanner.nextInt();
int CHOICE[] = new int[k];
for (int i = 0; i < k; i++) {
CHOICE[i] = i + 1;
fileWriter.write(CHOICE[i] + " ");
}
fileWriter.write("\n");
if (n != k) {
int cur[] = nextCombination(CHOICE, n, k);
while (cur != null) {
for (int j = 0; j < k; j++) {
fileWriter.write(cur[j] + " ");
}
cur = nextCombination(cur, n, k);
fileWriter.write("\n");
}
}
fileWriter.flush();
}
}