Hust oj 1945 纸牌小游戏(模拟水题)

纸牌小游戏
Time Limit: 1000 MSMemory Limit: 32768 K
Total Submit: 109(76 users)Total Accepted: 85(73 users)Rating: Special Judge: No
Description

小A和小B在玩纸牌游戏,游戏的规则是:

小A使用栈,小B使用队列,给出一个长度为n的数列,从数列左边到数列右边依次入栈/入队,其中穿插m次出栈/出队操作。

最后,谁栈(队列)中的所剩数字的总和大,谁就是winner。(保证最终有一人胜出)

Input


多组测试数据。

每组数据第一行有2个整数n,m(0<m<n<=1000)。

接下来是一个长度为n的整数序列(每个整数都在int范围内)。

接下来n+m行,每行都有下2个操作中的一个:

push 对于小A来说是入栈,对于小B来说是入队

pop  对于小A来说是出栈,对于小B来说是出队

Output

每组测试数据输出一个整数,小A赢了的话输出1,否则输出2。

Sample Input
5 3
3 5 9 4 2
push
pop
push
push
pop
push
push
pop
Sample Output
1
模拟
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstring>
using namespace std;

const int Maxn = 1005;
int a[Maxn];
char str[5];
int n,m;

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        stack<int>s;
        queue<int>q;
        int sum_1 = 0;
        int sum_2 = 0;
        int index = 0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=0;i<n+m;i++)
        {
            scanf("%s",&str);
            if(strcmp(str,"push") == 0)
            {
                int temp = a[index++];
                s.push(temp);
                sum_1 += temp;
                q.push(temp);
                sum_2 += temp;
            }
            else
            {
                sum_1 -= s.top();
                s.pop();
                sum_2 -= q.front();
                q.pop();
            }
        }
        if(sum_1 > sum_2)
            printf("1\n");
        else
            printf("2\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值