<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">题目描述</span>
<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">题目描述</span>
桌子上有一碟牌,从第一张开始从上往下编号依次为1~n。当至少还有两张牌的时候进行以下操作:把第一张牌扔掉,把新的第一张牌放到最后。
输入n,输出每次扔掉的牌和最后剩下的牌。
输入
n张牌。
输出
每次扔掉的牌和最后剩下的牌。
样例输入
7
样例输出
1 3 5 7 4 2 6
解题思路
先进先出为队列,用数组实现队列;
代码
#include<cstdio>
using namespace std;
int q[100];
int head=1,tail=1;
void push(int n)
{
q[tail++]=n;
}
void pop()
{
head++;
}
int front()
{
return q[head];
}
int empty()
{
if(tail<=head)
return 1;
else
return 0;
}
int main()
{
int n,i;
scanf("%d",&n);
for(i=1;i<=n;++i)
{
q[i]=i;
tail++;
}
while(!empty())
{
printf("%d ",front());
pop();
push(front());
pop();
}
return 0;
}