一副从1到n的牌,每次从牌堆顶取一张放桌子上,再取一张放牌堆底,直到手机没牌,最后桌子上的牌是从1到n有序,设计程序,输入n,输出牌堆的顺序数组
public static void init(int n) {
LinkedList<Integer> a = new LinkedList<>();
LinkedList<Integer> b = new LinkedList<>();
for (int i = 0; i < n; i++) {
b.add(i + 1);
}
reverse(a, b);
}
private static void reverse(LinkedList<Integer> a, LinkedList<Integer> b) {
int c = b.pollLast();
a.push(c);
if (b.size() == 0) {
System.out.println(a);
return;
}
int d = a.pollLast();
a.push(d);
reverse(a, b);
}
public static void main(String[] args) {
init(6);
}