您的位置:首页 > 其它

ZCMU—1793

2017-01-11 19:23 183 查看

Problem D: Problem D - Snap

Time Limit: 1 Sec  Memory Limit:
128 MB
Submit: 5  Solved: 3

[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.

【分析】

题目很简单,有两个人,每个人给你一个牌的序列,每个人同时把自己手上的牌放到桌上,如果有一个时刻两个人拿出的牌是一样的,那么就能进行snap,由题目里给出的方法来判定谁进行snap,进行snap的人会获得另外那个人已经放在桌上的牌,把另外那个人已经放在桌上的牌整叠放到自己桌上的那堆牌上面



如果当前一个人手上的牌用完了,那就把放在桌上的牌倒过来拿在手上,也就是比如上图的BCBA,如果把这四张牌拿到手上,那么再次拿出手的顺序是ABCB。

然后一直模拟,直到有一个人的手牌为0那么这个人就输了,如果模拟次数达到1000次那么就输出"Keeps going and going ..."

然后判断谁进行操作的命令是int t=rand()/141%2;如果t==0那么就是jane进行操作,否则就是john进行操作。

rand头文件是<stdlib.h>

题意就是这样,剩下的就是模拟的事情了...我是直接用Vector进行模拟的,因为可以直接进行拼接和删除,怎么模拟都行反正最多只有50张牌不会超时。

【代码】

#include <iostream>
#include <cstdio>
#include <vector>
#include <time.h>
#include <stdlib.h>
using namespace std;
vector<char> a,b,aa,bb;
void init()
{
char s[1000];a.clear();aa.clear();b.clear();bb.clear();
gets(s);
for (int i=0;s[i]!='\0';i++) a.push_back(s[i]);
gets(s);
for (int i=0;s[i]!='\0';i++) b.push_back(s[i]);
}
int x;
char s[2000];
int main()
{
init();//srand(time(NULL));
vector<char> :: iterator ita,itb,itaa,it;
for (int pp=0;pp<1000;pp++)
{
if (a.size()==0){printf("John wins.\n");goto out;}
if (b.size()==0){printf("Jane wins.\n");goto out;}
int ttt=min(a.size(),b.size());
while(ttt--)
{
if (a.size()==0){printf("John wins.\n");goto out;}
if (b.size()==0){printf("Jane wins.\n");goto out;}
aa.push_back(a.front());
bb.push_back(b.front());
if (a.front()==b.front())
{
int t=rand()/141%2;
if (!t)
{
aa.insert(aa.end(),bb.begin(),bb.end());
bb.clear();
x=0;
for (ita=aa.begin();ita!=aa.end();ita++) s[x++]=*ita;
printf("Snap! for Jane: ");
while(x--) printf("%c",s[x]);
printf("\n");
}
else
{
bb.insert(bb.end(),aa.begin(),aa.end());
aa.clear();
x=0;
for (ita=bb.begin();ita!=bb.end();ita++) s[x++]=*ita;
printf("Snap! for John: ");
while(x--) printf("%c",s[x]);
printf("\n");
}
}
a.erase(a.begin());
b.erase(b.begin());
}
if (a.empty()) a=aa,aa.clear();
if (b.empty()) b=bb,bb.clear();
}
printf("Keeps going and going ...\n");
out:;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: