rambless
火车进站
https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static List<String> list = new ArrayList<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
list.clear();
int n = in.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++) {
arr[i] = in.nextInt();
}
Deque<Integer> stack = new LinkedList<>();
out(arr, stack, 0, 0, "");
Collections.sort(list);
for(String s : list) {
System.out.println(s);
}
}
}
private static void out(int[] arr, Deque<Integer> stack, int i, int n, String str) {
if(n==arr.length) {
list.add(str);
}
if(!stack.isEmpty()) {
int pop = stack.pop();
out(arr, stack, i, n+1, str+pop+" ");
stack.push(pop);
}
if(i<arr.length) {
stack.push(arr[i]);
out(arr, stack, i+1, n, str);
stack.pop();
}
}
}