-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combinations.java
50 lines (46 loc) · 1.36 KB
/
Combinations.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
/*
*/
import java.io.*;
import java.util.*;
public class Combinations {
private static List<List<Integer>> result = new ArrayList<List<Integer>>();
public static List<List<Integer>> combine(int n, int k) {
if(n < k) return result;
List<Integer> r = new ArrayList<>();
comRe(n, k, 1, r);
return result;
}
public static void comRe(int n, int k, int startSearchPos, List<Integer> r){
int m = r.size();
if(m == k){
result.add(new ArrayList<Integer>(r));
return;
}
for(int i = startSearchPos; i <= (n-k+1+m); i++){
r.add(i);
comRe(n, k, i+1, r);
r.remove(r.size()-1);
}
return;
}
public static void dispArray2D(List<List<Integer>> res){
for(int i = 0; i < res.size(); i++){
for(int j = 0; j < res.get(i).size(); j++){
System.out.print(res.get(i).get(j) + " "); // note: res[i][j]
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Enter k: ");
int k = reader.nextInt();
System.out.println("Enter n (n>k): ");
int n = reader.nextInt();
System.out.println("combinations using " + k + " numbers from 1 to " + n);
List<List<Integer>> r = combine(n, k);
System.out.println("results: ");
dispArray2D(r);
return;
}
}