【校队排位赛#2 C】 队列 模拟题

78 篇文章 0 订阅
10 篇文章 0 订阅

在这里插入图片描述

题意:两个人有各有一手牌,每人同时出最上面的那张牌,比较大的那个人可以吃掉对方牌同时把自己的一起放到最后,问最后谁能(让对方空牌)或者谁都不会赢

思路:

很直观的一个队列题,每次双方都取出队列顶部元素,同时pop掉,然后进行判断,将两张牌push按对手牌先入队列的方式push进大的一方队尾,重复这个操作,看谁空了就好了。
因为可能会一直这样进行下去,所以就设置turn回合大于某个特定大的数就break掉即可。

#include <iostream>
#include <vector>
#include <cstdio>
#include <map>
#include <climits>
#include <string>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#define maxn 105
using namespace std;
typedef  long long ll;
int main()
{
    int n;
    queue<int>  a;
    queue <int> b;
    cin>>n;

    int len;
    cin>>len;
    for(int i=1;i<=len;i++)
    {
        int tmp;
        cin>>tmp;
        a.push(tmp);
    }
    cin>>len;
    for(int i=1;i<=len;i++)
    {
        int tmp;
        cin>>tmp;
        b.push(tmp);
    }
    int turn = 0;
    while(!a.empty()&&!b.empty())
    {
          int  one = a.front();
          a.pop();
          int other = b.front();
          b.pop();
        //  cout<<one<<' '<<other<<endl;
          if(one>other)
          {
              a.push(other);
              a.push(one);
          }
          else
          {
              b.push(one);
              b.push(other);
          }
          turn ++;
          if(turn>1e6)
          break;
    }
    if(a.empty())
    printf("%d %d\n",turn,2);
    else if(b.empty())
    printf("%d %d\n",turn,1);
    else
    printf("-1\n");

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值