1704 卡片游戏

1704 卡片游戏

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 白银 Silver
 
 
题目描述 Description

桌面上有一叠牌,从第一张牌(即位于顶面的牌)开始从上往下依次编号为1~n.当至少还剩两张排时进行一下操作:把第一张牌扔掉,然后把新的第一张牌放到整叠牌的最后。输入n。输出每次扔掉的牌,以及最后剩下的牌。。

输入描述 Input Description

输入n

输出描述 Output Description

输出每次扔掉的牌,以及最后剩下的牌

样例输入 Sample Input

7

样例输出 Sample Output

1 3 5 7 4 2 6

本来在做图论题,没想到蹦出个这个来,呵呵

水题,一个队列就能搞定。

#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
int n,t,num=0;
queue<int>a;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++) a.push(i);
    while(num<n-1)
    {
        printf("%d ",a.front());
        a.pop();
        t=a.front();
        a.pop();
        a.push(t);
        num++;
    }
    printf("%d
",a.front());
    return 0;
}
代码
原文地址:https://www.cnblogs.com/EvilEC/p/6045958.html