经典第五章 习题 5-3 UVA 10935 Throwing cards away I(队列的简单应用)

题目:http://7xjob4.com1.z0.glb.clouddn.com/bc8f4a27cb57cafb184320d2484b0cda

就是简单的队列模拟。
下面是AC代码:

#include
#include
#include
#include
using namespace std;

int a[50];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==0)
        {
            continue;
        }
        if(n==1)
        {
            printf("Discarded cards:\n");
            printf("Remaining card: 1\n");
            continue;
        }
        queue<int>q;
        for(int i=1;i<=n;i++)
        {
            q.push(i);
        }
        int sum1=0,sum2;
        while(!q.empty())
        {
            a[sum1++]=q.front();
            q.pop();
            if(!q.empty())
            {
                int num=q.front();
                q.pop();
                if(!q.empty())
                {
                    q.push(num);
                }
                else
                {
                    sum2=num;
                    break;
                }
            }
        }
        printf("Discarded cards: 1");
        for(int i=1;iprintf(", %d",a[i]);
        }
        printf("\n");
        printf("Remaining card: %d\n",sum2);
    }
    return 0;
}

你可能感兴趣的:(UVA,刘汝佳-算法入门经典,经典第五章,数据结构)