ZCMU-1793-Snap

1793: Problem D - Snap

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 6   Solved: 4
[ Submit][ Status][ Web Board]

Description

Problem D - Snap

Snap is a 2-player card game. The deck of cards contains several of each type of card. Initially each player has one half of the deck, in some random sequence, face down in a pile, and plays them in sequence from top to bottom, placing each card face-up in another pile. When the face-down pile is exhausted, the face-up pile is turned over to become the face-down pile and play continues.

The two players play their cards concurrently and synchronously. That is, each places a card face up at exactly the same time. If two cards turned up at the same time are the same type, each player calls "Snap!" and the player who calls first takes the other's face-up pile and places it on top of his or her own.

Play proceeds until one player has all the cards. This player is the winner.

Your job is to simulate a game of snap to determine whether it will end within 1000 turns and, if so, to determine the winner.

Each type of card is denoted by a single letter or digit. The first line of input Jane's initial pile of cards, from top to bottom. The second line of input is John's initial pile. Jane and John start with the same number of cards; not more than 50 each.

During play, whenever it comes time to call "Snap!" the builtin function "random" is used to determine who calls first: if the expression

   random()/141%2           {in C or C++}

   random div 141 mod 2     {in Pascal; see note below}

yields 0, Jane calls first; otherwise John calls first. Whenever Jane calls first, print "Snap! for Jane: " followed by Jane's face-up pile, from top to bottom. Whenever John calls first, print "Snap! for John: " followed by John's face-up pile. If the game ends, print "John wins." or "Jane wins.", whichever is appropriate. If the game does not end when each player has turned over 1000 cards, print "Keeps going and going ..."

Input

Output

Sample Input

ABCDA
CBADC

Sample Output

Snap! for Jane: BCBA
Snap! for Jane: DADCBCBA
Snap! for John: CBAC
Snap! for John: ADADCBAC
John wins.

【解析】
这道题的意思其实就是给你两个人的牌数,以及牌的种类,如果两个人出牌的种类不一样就放桌子上,如果一样就把
某人桌上的牌给另外一个人,那这个某人怎么判断呢,根据(rand()/141)%2如果为0就把john桌上的牌给jane,这里举
个例子ABCDA和CBADC,再遇到第2张牌的时候相同了,就把CBADC中的CB放到AB上面,注意是这么放的把C放到AB上面,
再把B放到ABC的上面,输出是从最顶上开始输出。这样我们可以用vector来做,删除和插入和牌的移动就比较简单了。
一直到一个人的手牌为0,那个人就输了,还有就是如果一个人手上的手牌没有了,就把桌上的牌补充为手牌,这样一直
做,如果次数到了1000次就输出Keeps going and going ... 这里有一个地方学到的挺好的就是goto的应用,起来可以
很方便。
                                                                              
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<algorithm>
using namespace std;
vector<char> a,b,c,d;
int main()
{
    int i,n,m,len;
    char s[2017];
    a.clear();
    b.clear();
    c.clear();
    d.clear();
    scanf("%s",s);
    for(i=0;s[i]!='\0';i++)
        a.push_back(s[i]);//输入牌的种类
    scanf("%s",s);
    for(i=0;s[i]!='\0';i++)
        b.push_back(s[i]);
    vector<char>::iterator it;
    for(i=0;i<1000;i++)
    {
        if(a.size()==0)
        {
            printf("John wins.\n");
            goto out;
        }//当Jane手牌为0了
       else if(b.size()==0)
        {
           printf("Jane wins.\n");
           goto out;
        }
        len=min(a.size(),b.size());//手牌数量以小的为准进行比较
        while(len--)
        {
        if(a.size()==0)
        {
            printf("John wins.\n");
            goto out;
        }
       else if(b.size()==0)
        {
           printf("Jane wins.\n");
           goto out;
        }
        c.push_back(a.front());
        d.push_back(b.front());
        if(a.front()==b.front())//比较两个手牌一不一样
        {
            m=(rand()/141)%2;//决定是谁Snap
            if(m==0)
            {
                c.insert(c.end(),d.begin(),d.end());
                d.clear();
                n=0;
                for(it=c.begin();it!=c.end();it++)
                {
                    s[n++]=*it;
                }
                printf("Snap! for Jane: ");
                while(n--)
                    printf("%c",s[n]);
                printf("\n");
            }
            else
            {
                d.insert(d.end(),c.begin(),c.end());
                c.clear();
                n=0;
                for(it=d.begin();it!=d.end();it++)
                {
                    s[n++]=*it;
                }
                printf("Snap! for John: ");
                while(n--)
                    printf("%c",s[n]);
                printf("\n");
            }
        }
        a.erase(a.begin());//删除第一个元素,每比较一次删除一次
        b.erase(b.begin());
    }
    if(a.empty())
        a=c,c.clear();//如果手牌空了,就把桌上的牌给他
    if(b.empty())
        b=d,d.clear();
    }
    printf("Keeps going and going ...\n");
    out:;
    return 0;
}

基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip 个人大四的毕业设计、课程设计、作业、经导师指导并认可通过的高分设计项目,评审平均分达96.5分。主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 [资源说明] 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设或者课设、作业,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96.5分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),供学习参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值