1793: Problem D - Snap
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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
Sample Output
#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;
}