题目:
桌上有一叠牌,从第一张牌(即位于顶面的牌)开始从上往下依次编号1~n。
当至少还剩下两张牌时进行以下操作:
把第一张牌扔掉,然后把新的第一张牌放到整叠牌的最后,输入n,输出每次扔掉的牌,以及最后剩下的牌。
样例输入: 7
样例输出: 1 3 5 7 4 2 6
解:
public class TryOneTry {
static Queue<Integer> q = new LinkedList<>();
public static void fun() {
System.out.print(q.poll() + " ");
if (!q.isEmpty())
q.add(q.poll());
}
public static void main(String[] args) {
Scanner out = new Scanner(System.in);
int n = out.nextInt();
for (int i = 1; i <= n; i++)
q.add(i);
while (!q.isEmpty())
fun();
}
}