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

2个回答

 #include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<string.h>
#include<string.h>
#include<memory.h>
using namespace std;
int sg[50];
int num[7];
bool dfs(int sum)
{
    if(sum>=31) return 0;
    for(int i=1;i<=6;i++)
    {
        if(num[i]&&sum+i<=31)
        {
            --num[i];
            if(dfs(sum+i)==0) {++num[i];return 1;}
             ++num[i];//回溯
        }
    }
    return 0;
}
int main()
{
    char str[35];
    while(scanf("%s",str)!=EOF)
    {
        int sum=0;
        int l=strlen(str);
        for(int i=1;i<=6;i++)
        num[i]=4;
        for(int i=0;i<l;i++)
        {
            sum+=str[i]-'0';
            num[str[i]-'0']--;
        }
        if(sum>=31)
        {
            printf("%s ",str);
            if(l&1) puts("A");
            else puts("B");
            continue;
        }
        printf("%s ",str);
        if(dfs(sum))
        {
            if(l&1)  puts("B");
            else puts("A");
        }
        else
        {
         if(l&1)  puts("A");
         else puts("B");
       }
    }
}
/*
356656
35665
3566
111126666
552525
*/

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
The Game of 31
The game of 31 was a favorite of con artists who rode the railroads in days of yore. The game is played with a deck of 24 cards: four labeled 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 stratefy.nFor example, in the following game player B wins:nnPlayer A plays 3nPlayer B plays 5nPlayer A plays 6nPlayer B plays 6nPlayer A plays 5nPlayer B plays 6nnnInputnnThe 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.nnnOutputnnFor 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.nnnSample Inputnn356656n35665n3566n111126666n552525nnnSample Outputnn356656 Bn35665 Bn3566 An111126666 An552525 An
android 最新31个游戏合集 game 31个_2.rar
android 最新31个游戏合集 game 31.rar 第二个包
android 最新31个游戏合集 game 31个_3.rar
第三个包,android 游戏 game G3 G2之前已经描述了
game game game game game
很好玩的小游戏,适合上班时间消遣!有空下了玩玩吧!
android 最新31个游戏合集 game 31.rar
数独 描述:机动拼图 描述:细胞战争 描述:发达三国斗地主 描述:Lightracer3d 3D赛车游戏 描述:魔兽世界信息 描述:狂热格斗 描述:Kakuro 类似于扫雷的游戏 描述:山寨版的吃豆人 描述:Gem Buster 又一款宝石连连看 ==只下载了31个打包方便下载,需要的下吧;可以去论坛一个一个的下载weiphone.com 一次只能上传15M...分批上传
Game
Problem Descriptionn  Nowadays, there are more and more challenge game on TV such as 'Girls, Rush Ahead'. Now, you participate int a game like this. There are N rooms. The connection of rooms is like a tree. In other words, you can go to any other room by one and only one way. There is a gift prepared for you in Every room, and if you go the room, you can get this gift. However, there is also a trap in some rooms. After you get the gift, you may be trapped. After you go out a room, you can not go back to it any more. You can choose to start at any room ,and when you have no room to go or have been trapped for C times, game overs. Now you would like to know what is the maximum total value of gifts you can get.n nnInputn  The first line contains an integer T, indicating the number of testcases.n  For each testcase, the first line contains one integer N(2 <= N <= 50000), the number rooms, and another integer C(1 <= C <= 3), the number of chances to be trapped. Each of the next N lines contains two integers, which are the value of gift in the room and whether have trap in this rooom. Rooms are numbered from 0 to N-1. Each of the next N-1 lines contains two integer A and B(0 <= A,B <= N-1), representing that room A and room B is connected.n  All gifts' value are bigger than 0.n nnOutputn  For each testcase, output the maximum total value of gifts you can get.n nnSample Inputn2n3 1n23 0n12 0n123 1n0 2n2 1n3 2n23 0n12 0n123 1n0 2n2 1n nnSample Outputn146n158
The Game
DescriptionnnOne morning, you wake up and think: "I am such a good programmer. Why not make some money?'' So you decide to write a computer game. nThe game takes place on a rectangular board consisting of w * h squares. Each square might or might not contain a game piece, as shown in the picture. nnOne important aspect of the game is whether two game pieces can be connected by a path which satisfies the two following properties: nnIt consists of straight segments, each one being either horizontal or vertical. nnnIt does not cross any other game pieces. nn(It is allowed that the path leaves the board temporarily.) nnHere is an example: nnnThe game pieces at (1,3) and at (4, 4) can be connected. The game pieces at (2, 3) and (3, 4) cannot be connected; each path would cross at least one other game piece. nnThe part of the game you have to write now is the one testing whether two game pieces can be connected according to the rules above.nInputnnThe input contains descriptions of several different game situations. The first line of each description contains two integers w and h (1 <= w,h <= 75), the width and the height of the board. The next h lines describe the contents of the board; each of these lines contains exactly w characters: a "X" if there is a game piece at this location, and a space if there is no game piece. nnEach description is followed by several lines containing four integers x1, y1, x2, y2 each satisfying 1 <= x1,x2 <= w, 1 <= y1,y2 <= h. These are the coordinates of two game pieces. (The upper left corner has the coordinates (1, 1).) These two game pieces will always be different. The list of pairs of game pieces for a board will be terminated by a line containing "0 0 0 0". nnThe entire input is terminated by a test case starting with w=h=0. This test case should not be procesed.nOutputnnFor each board, output the line "Board #n:", where n is the number of the board. Then, output one line for each pair of game pieces associated with the board description. Each of these lines has to start with "Pair m: ", where m is the number of the pair (starting the count with 1 for each board). Follow this by "ksegments.", where k is the minimum number of segments for a path connecting the two game pieces, or "impossible.", if it is not possible to connect the two game pieces as described above. nnOutput a blank line after each board.nSample Inputnn5 4nXXXXXnX XnXXX Xn XXX n2 3 5 3n1 3 4 4n2 3 3 4n0 0 0 0n0 0nSample OutputnnBoard #1:nPair 1: 4 segments.nPair 2: 3 segments.nPair 3: impossible.
拖拉机game 拖拉机game
game 拖拉机game 拖拉机game 拖拉机
game
一个用tc++写的游戏,有源代码可看。里面有很多不足,请多多批评,指教。
Game?
请问如果想成为一个职业的游戏程序员,刚开始要学习哪方面的知识?谢谢!
Game Mechanics Advanced Game Design
Game Mechanics Advanced Game Design
Game Mechanics: Advanced Game Design
Game Mechanics: Advanced Game Design (Voices That Matter) By 作者: Ernest Adams – Joris Dormans ISBN-10 书号: 0321820274 ISBN-13 书号: 9780321820273 Edition 版本: 1 Release 出版日期: 2012-06-25 pages 页数: (360 )
fish game a simall game
一个用java swing 写的小游戏, 参照了捕鱼达人的游戏规则!整体风格捕鱼达人一样
Game Mechanics - Advanced Game Design
游戏机制 - 高级游戏设计,游戏设计的圣经。
网络技术基础31 网络技术基础31 网络技术基础31 网络技术基础31
网络技术基础31 网络技术基础31 网络技术基础31 网络技术基础31
31 Activiti
教程视频:Activiti是业务流程管理(BPM)框架,它是覆盖了业务流程管理,工作流,服务协作等领域的一个开源,灵活的,易扩展的可执行流程语言框架。
css+div 31
css+div ....................
面试题 31
1 题目描述输入一个整数数组,数组里有证正数也有负数,数组中一个或连续的多个整数组成一个子数组,求所有数组的和的最大值。要求时间复杂度为O(n)2 算法描述{1,-2,3,10,-4,7,2,-5} => {3,10,-4,7,2}1+(-2)=(-1) 如果后面的元素加上 -1 的话,只会让总和减小,所以当总和小于等于零的时候,我们需要抛弃之前所求得的和,从下一个元开始重新计算 3+10-
1月31
今天还是看ac自动机的博客,主要是看ac自动机加dp的题,ac自动机加矩阵的题也有很多,我没看。ac自动机虽然可以与很多种dp相结合,但还是有规律可循的。加上dp的时候更换了模板,不再用last数组优化了,而是将树的很多节点连接起来,这里不多说了。与概率dp相结合很容易看出来,因为有概率吗,然后也是字符串与数匹配的问题,这个倒是很容易判断出来,这类题运用的是fill数组与构建的match数组,来进
怎么把 Fri Jan 16 15:31:31 2004 变为只要15:31:31部分?
请教:rnfunction()rn ResultTime = (NowTime.getTime()-OrgTime.getTime());rn ResultTime = Date(ResultTime)rn Div1.innerHTML = ResultTime.toString();rnrnDiv1里显示的是 Fri Jan 16 15:31:31 2004 ,怎么把 Fri Jan 16 15:31:31 2004 变为只要15:31:31部分? rnrn
EDA技术31 EDA技术31 EDA技术31 EDA技术31
EDA技术31 EDA技术31 EDA技术31 EDA技术31
谷歌chrome 31
谷歌chrome 31离线安装包,google chrome offline installation file
总结31
A常量VS变量100% B#include using namespace std ;  int main(); { const int AMOUNT=100; int price=0; printf("请输入金额"); scanf("%d",&price); int change=AMOUNT-price; printf("找你%d元"\n,change) ; return
activexbar 31
activexbar 31,包含升级注册
31分了
delphi ADOExpress Update Pack 2 在教育网中从哪里下载
31练习
主要讲授面向过程和面向对象,以及面向对象的特性,以及Java内存分配
31总结
前端开发的程序员,我相信他们都会熟练地运用至少一套适合自己的UI控件,无论是用jQuery EasyUI还是其他的UI控件。重写的UI控件,主要体现在兼容性好,样式统一,如果你不用UI控件,用浏览默认的控件,它们的样式将不会统一呈现,在不同的浏览器显现的样式将会不同,这将会给你带来诸多的烦恼,所以学习一套UI的重要性可想而知。今天我带领大家好好的学习jQuery EasyUI。
31类型转换
<p>rn <br />rn</p>rn<p>rn <p>rn 20周年限定一卡通!<span style="color:#337FE5;">可学Java全部课程</span>,仅售799元(原价7016元),<span style="color:#E53333;">还送漫威正版授权机械键盘+CSDN 20周年限量版T恤+智能编程助手!</span>rn </p>rn <p>rn 点此链接购买:rn </p>rn <table>rn <tbody>rn <tr>rn <td>rn <span style="color:#337FE5;"><a href="https://edu.csdn.net/topic/teachercard?utm_source=jsk20xqy" target="_blank">https://edu.csdn.net/topic/teachercard?utm_source=jsk20xqy</a><br />rn</span>rn </td>rn </tr>rn </tbody>rn </table>rn</p>rn<span>&nbsp;</span>rn<p>rn 本教程讲解的内容是python的基础知识,教程中包含了操作系统,python环境搭建,python的基本语法、类型和对象、流程控制、函数、函数式编程、面向对象、文件操作等内容。本教程由浅入深,贴近真实开发,完全为python新手量身定做,也适合老手用来巩固补充基础知识,查漏补缺。rn</p>
爬虫-31
玩转Python开发系列(包括Python语言基础、Linux基础、数据库(MySQL、MongoDB、Redis)、前端、Django、Flask、Tornado、爬虫、微信公众号)目的是使学员可以成为Python初级开发工程师
失业第31天,散31分!
如题。
华为31
package NiukeBrush; import java.io.BufferedInputStream; import java.util.Scanner; //题目描述 // //从输入任意个整型数,统计其中的负数个数并求所有非负数的平均值 // //输入描述: //输入任意个整数 // // //输出描述: //输出负数个数以及所有非负数的平均值 // //输入例子...
31 相对定位
该系列课程主要讲解了div+css的理论基础知识,为大家以后的实战布局打下坚实的基础。课程通俗易懂,授课幽默,适合无相关学习经验的小白观看。
31作业
Python全栈视频培训教程,帮助开发者从零到一入门python:1.深入理解python语法机制与底层原理,2.深入实战python各种案例 , 3.语音识别,图形界面,多线程爬虫,语音合成,游戏控制等等精彩案例。
前端-31
玩转Python开发系列(包括Python语言基础、Linux基础、数据库(MySQL、MongoDB、Redis)、前端、Django、Flask、Tornado、爬虫、微信公众号)目的是使学员可以成为Python初级开发工程师
31章
31.1基础数论概念: 基础定理:1.除法定理:对于任何整数a和任何正整数n,存在唯一的q和r( 0 由此定理可以引申出一系列定义:商: ;余数:r = a mod b;a整除n:余数为0;n是a的倍数:r为0;公 约数:n整除a且a整除n;最大公约数:最大的公约数;     2.(公约数与线性组合)( d | a ) ∩ ( d | b ) → ( d | ax + by ) (∀
5月31
篮球 : 背身右转后仰跳投 背身左转勾手 可以加假动作 椅子训练法 突破时候,要观察补防人 持球突破,从组织到进攻,对手想往上扑,需要快速衔接突破,...
31、键盘事件
键盘事件:(只有能得到焦点的元素才能触发键盘事件) onkeydown onkeyup onkeypress : down 比 press先触发 / press只响应能输入值的键
31、条件判断
1、条件判断 1.1  方法一: test  -e  test.txt        echo $?     //0正确 1.2  方法二: [ -e test.txt ] echo $?     //0正确 1.3  方法三  : [ -d  /root ]  && echo 'yes' || echo 'no'            //是否是目录 [ -f  /root/t
(31)鸡尾酒排序
一种改进的冒泡排序,原理是对要排序的数组进行双向冒泡,双向冒泡排序又称鸡尾酒排序。 原理是:大数向后排,小数向前排。 代码: #include using namespace std; void CockTail(int num[],int n) { int tail = n - 1; int i; for ( i = 0; i < tail;) { //将最小的数排到前面 f
android-sdk-windows-1.5_r3.part 31
android-sdk-windows-1.5_r3.part 31
相关热词 c++和c#哪个就业率高 c# 批量动态创建控件 c# 模块和程序集的区别 c# gmap 截图 c# 验证码图片生成类 c# 再次尝试 连接失败 c#开发编写规范 c# 压缩图片好麻烦 c#计算数组中的平均值 c#获取路由参数