题目:手里有五张牌,桌上有一堆牌(五张)。你能够弃掉手中的k张牌,然后从牌堆中取最上面的k个。

比較规则例如以下:(按优先级排序)

1.straight-flush:同花顺。牌面为T(10) - A,这里不论花色是否同样;

2.four-of-a-kind:四条,牌面有4个同样的值。

3.full-house:船牌,牌面有3个同样值,剩下2个也同样值。

4.flush:同花,五张牌的花色同样,不是同花顺;

5.straight:顺子。五张牌的值连续,A能够作为1也能够作为14;

6.three-of-a-kind:三条,牌面有3个同样的值;

7.two-pairs:两对。牌面有2个对子。

8.one-pair:一对,牌面有一个对子,即2个同值;

9.highest-card:大牌,没有以上牌型。

分析:搜索、枚举。枚举换牌数从0-5的每种情况,推断,取出优先值最高的就可以。

说明:读题是重点。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio> #define min(x,y) ((x)<(y)? (x):(y)) using namespace std; char temp[5][3];
char card[10][3];
int maps[5][13]; char output[11][20] = {
"","straight-flush","four-of-a-kind",
"full-house","flush","straight","three-of-a-kind",
"two-pairs","one-pair","highest-card",""}; int value( char ch )
{
if ( ch == 'T' ) return 9;
if ( ch == 'J' ) return 10;
if ( ch == 'Q' ) return 11;
if ( ch == 'K' ) return 12;
if ( ch == 'A' ) return 0;
return ch - '1';
} int color( char ch )
{
if ( ch == 'S' ) return 0;
if ( ch == 'H' ) return 1;
if ( ch == 'D' ) return 2;
if ( ch == 'C' ) return 3;
} int tests()
{
//royal-flush | straight-flush
for ( int i = 0 ; i < 5 ; ++ i )
if ( maps[i][0]&maps[i][9]&maps[i][10]&maps[i][11]&maps[i][12] )
return 1;
//four-of-a-kind
for ( int i = 0 ; i < 13 ; ++ i )
if ( maps[4][i] == 4 ) return 2;
//full-house
int three = 0,two = 0;
for ( int i = 0 ; i < 13 ; ++ i ) {
if ( maps[4][i] == 2 ) two ++;
if ( maps[4][i] == 3 ) three ++;
}
if ( two && three ) return 3;
//flush
for ( int i = 0 ; i < 4 ; ++ i ) {
int count = 0;
for ( int j = 0 ; j < 13 ; ++ j )
count += maps[i][j];
if ( count >= 5 ) return 4;
}
//straight
for ( int i = 0 ; i < 10 ; ++ i )
if ( maps[4][i]&maps[4][i+1]&maps[4][i+2]&maps[4][i+3]&maps[4][(i+4)%13] )
return 5;
//three-of-a-kind
if ( three ) return 6;
//two-pairs
if ( two > 1 ) return 7;
//one-pair
if ( two ) return 8;
return 9;
} void change()
{
for ( int i = 0 ; i < 5 ; ++ i )
strcpy(temp[i],card[i+5]);
memset( maps, 0, sizeof(maps) );
for ( int i = 0 ; i < 5 ; ++ i ) {
maps[color(temp[i][1])][value(temp[i][0])] = 1;
maps[4][value(temp[i][0])] ++;
}
int Min = tests(); for ( int k = 1 ; k <= 5 ; ++ k ) {
int xx,yy,comb = (1<<k)-1;
while ( comb < 32 ) {
// 计算当前状态相应的集合
int j = 0,count = 0,move = 5;
do {
if ( (1<<j)&comb )
strcpy(temp[count ++],card[j]);
j ++;
}while ( j < 5 ); while ( count < 5 )
strcpy(temp[count ++],card[move ++]); memset( maps, 0, sizeof(maps) );
for ( int i = 0 ; i < 5 ; ++ i ) {
maps[color(temp[i][1])][value(temp[i][0])] = 1;
maps[4][value(temp[i][0])] ++;
} Min = min(Min,tests());
// 位运算计算下一组合
xx = comb&-comb,yy = comb+xx;
comb = ((comb&~yy)/xx>>1)|yy;
}
}
printf("%s\n",output[Min]);
} int main()
{
while ( ~scanf("%s",card[0]) ) {
for ( int i = 1 ; i < 10 ; ++ i )
scanf("%s",card[i]); printf("Hand: ");
for ( int i = 0 ; i < 5 ; ++ i )
printf("%s ",card[i]);
printf("Deck: ");
for ( int i = 5 ; i < 10 ; ++ i )
printf("%s ",card[i]);
printf("Best hand: ");
change();
}
return 0;
}

UVa 131 - The Psychic Poker Player的更多相关文章

  1. uva131 The Psychic Poker Player

    The Psychic Poker Player Time Limit: 3000MS     64bit IO Format: %lld & %llu Description In 5-ca ...

  2. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  3. 备战NOIP每周写题记录(一)···不间断更新

    ※Recorded By ksq2013 //其实这段时间写的题远远大于这篇博文中的内容,只不过那些数以百记的基础题目实在没必要写在blog上; ※week one 2016.7.18 Monday ...

  4. [IOS] Storyboard全解析-第一部分

    (Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明的说明Storyboard的效果,我贴上本教程所完成的Storyboard的截图: 现在,你就可以清楚的看到这 ...

  5. iOS Storyboard全解析

    来源:http://iaiai.iteye.com/blog/1493956 Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明的说明Storyboard的效果, ...

  6. iOS Storyboard 的基本用法

    (Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明的说明Storyboard的效果,我贴上本教程所完成的Storyboard的截图:  现 在,你就可以清楚的看 ...

  7. Java-->发牌流程修改版

    --> 这一次要封装得狠一点... package com.xm.ddz; // 每一张牌的属性 public class Card { private String flowerColor; ...

  8. 最新iOS 6 in Xcode4.5新特性——Storyboard和属性自动绑定

    最新iOS 6 in Xcode4.5新特性编程之二(上)——Storyboard和属性自动绑定 从Xcode 4.3开始,Storyboard 就是iOS 5和iOS 6中令人兴奋的一个新特性,他将 ...

  9. java练习:质数,匿名内部类创建接口,抽象类派生子类,画圆,字节截取字符串,数字变钱币,五子棋,梭哈

    java学习-质数的孤独 正在看质数的孤独,,,于是写了一个练习代码,输出1-100之间的质数 代码比较烂.待完善吧. 这里用到了continue和break,continue指结束当前轮次循环,跳入 ...

随机推荐

  1. 走迷宫(DFS)

    题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2449&cid=1181 目前dfs 里的递归还是不很懂,AC代码如下: #incl ...

  2. 大四实习准备2_java异常处理_android控件练习

    2015-4-24 Java 异常处理 可以有多个catch;ArrayIndexOutOfBoundsException类是Exception类的子类RuntimeException类的一个间接子类 ...

  3. 安装php 带debug

    gdb安装包   在CentOS6.4下使用gdb进行调试的时候, 使用bt(breaktrace)命令时,会弹出如下的提示: 头一天提示: Missing separate debuginfos, ...

  4. Microsoft强大团队(源代码)管理工具--TFS2010 与vs结合

    今天看了与vs 集成原理工具 TFS 2010, 角色分配.项目管理.开发源代码管理.任务分配管理.测试文档管理及跟踪等管理流程.代码版本的分支与合并等等,功能好强大啊. 以下将其安装配置简要介绍(以 ...

  5. JXL解析Excel表格内容到数据库

    java中常用的解析Excel表格的工具一种是POI一种是JXL,POI功能强大,相比JXL稍嫌复杂,对表格样式的处理非常好:而JXL解析简单方便,对中文支持比较好. 工作中解析Excel内容上传到数 ...

  6. 几种通讯协议的比较RMI > Httpinvoker >= Hessian >> Burlap >> web service

    一.综述本文比较了RMI,Hessian,Burlap,Httpinvoker,web service等5种通讯协议的在不同的数据结构和不同数据量时的传输性能.RMI是java语言本身提供的远程通讯协 ...

  7. shell脚本编写笔记

    包含转载内容,转载自http://wenku.baidu.com/link?url=jtCHxEYzgGve6P64U3JRQRgU6nhpGvqFLLpWu9I2Htq6hi9TGLudRFkk7r ...

  8. 任务栏窗口和状态图标的闪动 z

    Demo程序: 实现任务栏窗体和图标的闪动: 整个程序是基于Windows Forms的,对于任务栏右下角状态图标的闪动,创建了一个类型:NotifyIconAnimator,基本上是包装了Windo ...

  9. NGINX(五)模块

    nginx模块分为以下几类: NGX_CORE_MODULE //核心模块 NGX_HTTP_MODULE //HTTP处理模块 NGX_EVENT_MODULE //事件处理模块 NGX_MAIL_ ...

  10. Samung Galaxy III I535 黑砖救活经过

    昨天不小心把手机给搞成黑砖了,如下是昨天发帖求助,结果还没审核过,晕. http://bbs.zhiyoo.com/forum.php?mod=viewthread&tid=9673138&a ...