这两天终于记得写;了
用python用习惯了lambda,把java的函数式编程也搞起~
这题好像也不用状态压缩,不过写都写了嘛
想了下,不用状态压缩的原因是序列是顺序的,是有限的
import java.util.*;
import java.util.stream.*;
public class Main{
private Scanner jin = new Scanner(System.in);
private List<Integer> buffer = new ArrayList<>();
void run(){
int n = jin.nextInt();
dfs(n, 0, 0);
}
void dfs(int n, int status, int x){
if (status == (1 << n) - 1){
String res = String.join(" ", buffer.stream().map(y->String.valueOf(y+1)).collect(Collectors.toList()));
System.out.println(res);
} else {
dfs(n, status | (1 << x), x+1);
buffer.add(x);
dfs(n, status | (1 << x), x + 1);
buffer.remove(buffer.size() - 1 );
}
}
public static void main(String[] args){new Main().run();}
}