92. 递归实现指数型枚举(Acwing)
题目链接:
https://www.acwing.com/problem/content/94/
题解:
一个位置选或者不选两种情况,递归到边界(n个已经选完了),输出结果即可
AC代码:
import java.util.Scanner;
public class NO92 {
static int n;
static int N = 18;
static int[] visit = new int[N];
static void choose(int pos) {
if (pos >= n + 1) {
for (int i = 1;i <= n; i++) {
if (visit[i] > 0) {
System.out.print(i + " ");
}
}
System.out.println();
return;
}
visit[pos] = 1;
choose(pos+1);
visit[pos] = 0;
choose(pos+1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
choose(1);
}
}