队列(queue)的简单运用

题目描述:
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
样例输入:
abcb
aacb
bccc
样例输出:
C
题目大意:A B C 三个人每人一个字符串,该字符串只包含 abc,A先从串首拿出一个字符,并删除该字符,如果删除的字符是 a,下一轮还是A操作,如果是b,那么小一轮B操作,如果是c,下一轮C操作,同样其他人也按照这样的规则进行,最后谁的字符串先删完并再一次轮到他,谁赢,最后输出赢家。
需要注意的点:每次循环开始前要先判断相应字符串是否已经到达最后一个字符,然后在进行后面的操作,先判断,在操作
首先用数组来做:

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

string a,b,c;//存储三个字符串
int i = 0,j = 0,k = 0;//记录每个字符串读取字符的位置
//判断当要读取相应字符串时该字符串是否已经被读完
bool judge (char s,int ans) {
	if(s == 'a'&&ans >= a.length())
		return 0;
	if(s == 'b'&&ans >= b.length())
		return 0;
	if(s == 'c'&&ans >= c.length())
		return 0;
return 1;
}

int main() {

	cin >> a;
	cin >> b;
	cin >> c;

	char s = a[0];i++;
	while (i){//这里的i没有什么意义,就是为了不让while函数不空,因为while的参数不能为空
		if(s == 'a') {
			if(!judge(s,i))//先判断是否已经读完,若读完就跳出循环
				break;
			s = a[i++];//若没有读完就继续向后读取
		}
		if(s == 'b') {
			if(!judge(s,j))
				break;
		s = b[j++];
		}
		if(s == 'c') {
			if(!judge(s,k))
				break;
		s = c[k++];
		}
	}
	//最后只需要看s里面存储的值就可以判断谁先出完
	if(s == 'a')
		cout << "A" << "\n";
	if(s == 'b')
		cout << "B" << "\n";
	if(s == 'c')
		cout << "C" << "\n";
	return 0;

用队列就相应要麻烦一点,需要将数据度到字符串,在遍历字符串,将数据存储到队列里面,但是不用像字符串一样设置i,j,k来标记现在所读到的位置,我们只需要读一个然后出队就OK了,但是还是要先判断队列是否为空,在操作

#include <iostream>
#include <cstdio>
#include <string>
#include <queue>

using namespace std;

int main(){

	string a1,b1,c1;
	queue<char> a,b,c;

	cin >> a1;
	for(int i = 0;i < a1.length();i++)//先让每个元素入队
		a.push(a1[i]);

	cin >> b1;
	for(int i = 0;i < b1.length();i++)
		b.push(b1[i]);

	cin >> c1;
	for(int i = 0;i < c1.length();i++)
		c.push(c1[i]);
	
	char s = a.front();
	a.pop();//a先删除一个
	while(1)
	{
		if(s=='a')
		{
			if(a.empty())//若为空,输出,结束
			{
				printf("A\n");
				break;
			}
			s=a.front();//若不为空,更新v,并删除一个
			a.pop();
		}
		if(s == 'b')
		{
			if(b.empty())
			{
				printf("B\n");
				break;
			}
			s = b.front();
			b.pop();
		}
		if(s == 'c')
		{
			if(c.empty())
			{
				printf("C\n");
				break;
			}
			s = c.front();
			c.pop();
		}
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值