队列的简单应用

6 篇文章 0 订阅

Card Game for Three

题目描述:
Alice, Bob and Charlie are playing Card Game for Three, as below:
At first, each of the three players has a deck consisting of some number of cards. Each card has a letter a, b or c written on it. The orders of the cards in the decks cannot be rearranged.
The players take turns. Alice goes first.
If the current player’s deck contains at least one card, discard the top card in the deck. Then, the player whose name begins with the letter on the discarded card, takes the next turn. (For example, if the card says a, Alice takes the next turn.)
If the current player’s deck is empty, the game ends and the current player wins the game.
You are given the initial decks of the players. More specifically, you are given three strings SA, SB and SC. The i-th (1≦i≦|SA|) letter in SA is the letter on the i-th card in Alice’s initial deck. SB and SC describes Bob’s and Charlie’s initial decks in the same way.
Determine the winner of the game.
Constraints
1≦|SA|≦100
1≦|SB|≦100
1≦|SC|≦100
Each letter in SA, SB, SC is a, b or c.
输入:
The input is given from Standard Input in the following format:
SA
SB
SC
输出:
If Alice will win, print A. If Bob will win, print B. If Charlie will win, print C.
样例输入:
aca
accc
ca
样例输出:
A
题目大意:A B C 三个人每人一个字符串,该字符串只包含 abc,A先从串首拿出一个字符,并删除该字符,如果删除的字符是 a,下一轮还是A操作,如果是b,那么小一轮B操作,如果是c,下一轮C操作,同样其他人也按照这样的规则进行,最后谁的字符串先删完并再一次轮到他,谁赢,最后输出赢家。
博主的思路是建立三个队列,然后一个一个清理(轮到某人的时候先判断他的队列是否为空,若是,则输出其,结束)。

#include <bits/stdc++.h>
using namespace std;
int main()
{
    char aa[101],bb[101],cc[101],v;
    int i;
    queue<char> a,b,c;
    scanf("%s",aa);
    for(i=0;i<strlen(aa);i++)//先让每个元素入队
    {
        a.push(aa[i]);
    }
    scanf("%s",bb);
    for(i=0;i<strlen(bb);i++)
    {
        b.push(bb[i]);
    }
    scanf("%s",cc);
    for(i=0;i<strlen(cc);i++)
    {
        c.push(cc[i]);
    }
    v=a.front();
    a.pop();//a先删除一个
    while(1)
    {
        if(v=='a')
        {
            if(a.empty())//若为空,输出,结束
            {
                printf("A\n");
                break;
            }
            else//若不为空,更新v,并删除一个
            {
                v=a.front();
                a.pop();
            }
        }
        if(v=='b')
        {
            if(b.empty())
            {
                printf("B\n");
                break;
            }
            else
            {
                v=b.front();
                b.pop();
            }
        }
        if(v=='c')
        {
            if(c.empty())
            {
                printf("C\n");
                break;
            }
            else
            {
                v=c.front();
                c.pop();
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值