AC代码1:(普通数组,更好理解)
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
public class Main
{
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = 100010;
static boolean used[] = new boolean[N];
static int num[] = new int[N];
static int n,k;
// dfs存取的状态:pos表示当前该枚举哪个位置,startIndex表示最小可以从哪个数开始枚举
static void dfs(int pos, int startIndex)
{
if(pos == k + 1)
{
for(int i = 1 ; i <= k ; i ++) pw.print(num[i] + " ");
pw.println();
return;
}
for(int i = startIndex ; i <= n ; i ++)
{
if(!used[i])
{
num[pos] = i;
used[i] = true;
dfs(pos + 1, i + 1);
num[pos] = 0;
used[i] = false;
}
}
}
public static void main(String[] args) throws IOException
{
n = rd.nextInt();
k = rd.nextInt();
dfs(1,1);
pw.flush();
}
}
class rd
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String nextLine() throws IOException { return reader.readLine(); }
static String next() throws IOException
{
while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException { return Integer.parseInt(next()); }
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
static long nextLong() throws IOException { return Long.parseLong(next()); }
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(rd.nextLine());
return d;
}
}
class math
{
int gcd(int a,int b)
{
if(b == 0) return a;
else return gcd(b,a % b);
}
int lcm(int a,int b)
{
return a * b / gcd(a, b);
}
// 求n的所有约数
List get_factor(int n)
{
List<Long> a = new ArrayList<>();
for(long i = 1; i <= Math.sqrt(n) ; i ++)
{
if(n % i == 0)
{
a.add(i);
if(i != n / i) a.add(n / i); // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况 ^-^复杂度能减少多少是多少
}
}
// 相同因子去重,这个方法,完美
a = a.stream().distinct().collect(Collectors.toList());
// 对因子排序(升序)
Collections.sort(a);
return a;
}
// 判断是否是质数
boolean check_isPrime(int n)
{
if(n < 2) return false;
for(int i = 2 ; i <= n / i; i ++) if (n % i == 0) return false;
return true;
}
}
class PII implements Comparable<PII>
{
int x,y;
public PII(int x ,int y)
{
this.x = x;
this.y = y;
}
public int compareTo(PII a)
{
if(this.x-a.x != 0)
return this.x-a.x; //按x升序排序
else return this.y-a.y; //如果x相同,按y升序排序
}
}
class Edge
{
int a,b,c;
public Edge(int a ,int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
}
AC代码2:(动态数组)
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
public class Main
{
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = 100010;
static boolean used[] = new boolean[N];
static List<Integer> path = new LinkedList<>();
static int n,k;
static void dfs(int startIndex)
{
if(path.size() == k)
{
for(int i = 0 ; i < path.size(); i ++) pw.printf("%d ",path.get(i));
pw.println();
pw.flush();
return;
}
for(int i = startIndex ; i <= n ; i ++)
{
// 变长数组的add是每一次都是从变长数组的最后添加一个元素
path.add(i);
// 不用打标记的原因就是:只要选了一个,i ++,选的数字都是递增的,序列是递增的
used[i] = true; // 打上放心
dfs(i + 1);
// 恢复现场,弹出刚才添加到路径的数
path.remove(path.size() - 1);
used[i] = false;
}
}
public static void main(String[] args) throws IOException
{
n = rd.nextInt();
k = rd.nextInt();
dfs(1);
pw.flush();
}
}
class rd
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String nextLine() throws IOException { return reader.readLine(); }
static String next() throws IOException
{
while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException { return Integer.parseInt(next()); }
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
static long nextLong() throws IOException { return Long.parseLong(next()); }
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(rd.nextLine());
return d;
}
}
class math
{
int gcd(int a,int b)
{
if(b == 0) return a;
else return gcd(b,a % b);
}
int lcm(int a,int b)
{
return a * b / gcd(a, b);
}
// 求n的所有约数
List get_factor(int n)
{
List<Long> a = new ArrayList<>();
for(long i = 1; i <= Math.sqrt(n) ; i ++)
{
if(n % i == 0)
{
a.add(i);
if(i != n / i) a.add(n / i); // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况 ^-^复杂度能减少多少是多少
}
}
// 相同因子去重,这个方法,完美
a = a.stream().distinct().collect(Collectors.toList());
// 对因子排序(升序)
Collections.sort(a);
return a;
}
// 判断是否是质数
boolean check_isPrime(int n)
{
if(n < 2) return false;
for(int i = 2 ; i <= n / i; i ++) if (n % i == 0) return false;
return true;
}
}
class PII implements Comparable<PII>
{
int x,y;
public PII(int x ,int y)
{
this.x = x;
this.y = y;
}
public int compareTo(PII a)
{
if(this.x-a.x != 0)
return this.x-a.x; //按x升序排序
else return this.y-a.y; //如果x相同,按y升序排序
}
}
class Edge
{
int a,b,c;
public Edge(int a ,int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
}