AcWing 93. 递归实现组合型枚举
原题链接
简单
作者:
疯子orz
,
2024-04-10 19:30:24
,
所有人可见
,
阅读 5
package com.zy.Acwing.dfs;
import java.util.Scanner;
/**
* 组合型:312与321是同一种情况
* 不需要状态数组来表示这个数是否用过
* @author abc
*
*/
public class 组合型枚举 {
static int m;
static int n;
static int[] a;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
m = scan.nextInt();
a = new int[n + 1];
dfs(1,1);//第二个数是表示从哪个数开始
}
private static void dfs(int u,int start) {
// TODO Auto-generated method stub
if(u > m) {
for(int i = 1;i <= m;i++) {
System.out.print(a[i] + " ");
}
System.out.println();
return;
}
for(int i = start;i <= n;i++) {
a[u] = i;
dfs(u + 1,i + 1);
a[u] = 0;
}
}
}