# hdu 4155 The Game of 31

Problem Description

The game of 31 was a favourite of con artists who rode the railroads in days of yore. The game is played with a deck of 24 cards: four labelled each of 1, 2, 3, 4, 5, 6. The cards in the deck are visible to both players, who alternately withdraw one card from the deck and place it on a pile. The object of the game is to be the last player to lay a card such that the sum of the cards in the pile does not exceed 31. Your task is to determine the eventual winner of a partially played game, assuming each player plays the remainder of the game using a perfect strategy.
For example, in the following game player B wins:
Player A plays 3
Player B plays 5
Player A plays 6
Player B plays 6
Player A plays 5
Player B plays 6


Input

The input will consist of several lines; each line consists of a sequence of zero or more digits representing a partially completed game. The first digit is player A's move; the second player B's move; and so on. You are to complete the game using a perfect strategy for both players and to determine who wins.


Output

For each game, print a line consisting of the input, followed by a space, followed by A or B to indicate the eventual winner of the game.


Sample Input

356656
35665
3566
111126666
552525


Sample Output

356656 B
35665 B
3566 A
111126666 A
552525 A


【题意】

【分析】

【代码】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<sstream>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS_1(a) memset(a,-1,sizeof(a))
#define MSinf(a) memset(a,0x3f,sizeof(a))
#define sin1(a) scanf("%d",&(a))
#define sin2(a,b) scanf("%d%d",&(a),&(b))
#define sll1(a) scanf("%lld",&(a))
#define sll2(a,b) scanf("%lld%lld",&(a),&(b))
#define sdo1(a) scanf("%lf",&(a))
#define sdo2(a,b) scanf("%lf%lf",&(a),&(b))
#define inf 0x3f3f3f3f
//#define lson i<<1,l,mid
//#define rson ((i<<1)|1),mid+1,r
#define uint unsigned int
typedef pair<int,int> PII;
#define A first
#define B second
#define pb push_back
#define MK make_pair
#define ll long long
template<typename T>
T x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
m = x*f;
}
template<typename T>
}
template<typename T>
void read3(T &a,T &b,T &c) {
}
template<typename T>
void out(T a) {
if(a>9) out(a/10);
putchar(a%10+'0');
}
template<typename T>
void outn(T a) {
if(a>9) out(a/10);
putchar(a%10+'0');
puts("");
}
///------------------------------------------------------------------------------------
char s[50];
int num[10];
int getsg(int a)
{
if(a>31) return 0;
rep1(i,1,6)
if(num[i]&&a+i<=31)
{
num[i]--;
if(getsg(a+i)==0)
{
num[i]++;
return 1;
}
num[i]++;
}
return 0;
}
int main() {
//    freopen("in.txt","r",stdin);
while(scanf("%s",s)!=EOF)
{
printf("%s ",s);
fill(num,num+10,4);
int ans = 0;
int len=strlen(s);
rep0(i,0,len)
{
int t = s[i] - '0';
num[t]--;
ans+=t;
}
if(getsg(ans)) puts(len&1?"B":"A");
else puts(len&1?"A":"B");
/*
后来还看到了一个简化版本，写的十分飘逸
if(getsg(ans)) printf("%c\n",'A'+(len&1));
else printf("%c\n",'B'-(len&1));
*/
}
return 0;
}
• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

zuhiul

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
09-11 2079