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;
}

教学辅助平台的出现,是为了更好地服务于教育工作者和学生,提高教学效果和学习效率。该平台集成了多个功能模块,旨在为用户提供全面、便捷的教学辅助服务。 平台首页作为导航入口,提供了清晰的界面布局和便捷的导航功能,方便用户快速找到所需功能。需要注意的是,“首页&rdquo;这一选项在导航菜单中出现了多次,可能是设计上的冗余,需要进一步优化。 “个人中心&rdquo;模块允许用户查看和管理自己的个人信息,包括修改密码等账户安全设置,确保用户信息的准确性和安全性。 在教育教学方面,“学生管理&rdquo;和“教师管理&rdquo;模块分别用于管理学生和教师的信息,包括学生档案、教师资料等,方便教育工作者进行学生管理和教学安排。同时,“课程信息管理&rdquo;、“科目分类管理&rdquo;和“班级分类管理&rdquo;模块提供了课程信息的发布、科目和班级的分类管理等功能,有助于教育工作者更好地组织和管理教学内容。 此外,“课程作业管理&rdquo;模块支持教师布置和批改作业,学生可以查看和提交作业,实现了作业管理的线上化,提高了教学效率。而“交流论坛&rdquo;模块则为学生和教师提供了一个交流和讨论的平台,有助于促进师生互动和学术交流。 最后,“系统管理&rdquo;模块为平台管理员提供了系统配置.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值