题目描述
java递归
样例
import java.util.Scanner;
public class Main {
static final int N = 15;
static int n;
static int[] st = new int[N + 1]; // 数组大小增加1以适应从1开始索引
public static void dfs(int u) {
if (u > n) {
for (int i = 1; i <= n; i++) {
if (st[i] == 1) {
System.out.print(i + " ");
}
}
System.out.println();
return;
}
st[u] = 1;
dfs(u + 1);
st[u] = 0;
st[u] = 2;
dfs(u + 1);
st[u] = 0;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
dfs(1);
scanner.close(); // 关闭scanner对象
}
}