-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubset.java
More file actions
35 lines (30 loc) · 707 Bytes
/
Subset.java
File metadata and controls
35 lines (30 loc) · 707 Bytes
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
package Recursion;
import java.util.Scanner;
public class Subset {
static int n;
static int[] ch;
public void dfs(int l) {
if (l == n + 1) {
String tmp = "";
for (int i = 1; i <= n; i++) {
if (ch[i] == 1) {
tmp += (i + " ");
}
}
if (tmp.length() > 0) {
System.out.println(tmp);
}
}else{
ch[l] = 1;
dfs(l + 1);
ch[l] = 0;
dfs(l + 1);
}
}
public void main() {
Scanner in = new Scanner(System.in);
n = in.nextInt();
ch = new int[n + 1];
dfs(1);
}
}