ACM: Poker Game

题目描述

A poker deck contains 52 cards. Each card has a suit of either clubs, diamonds, hearts, or spades (denoted C, D, H, S in the input data). Each card also has a value of either 2 through 10, jack, queen, king, or ace (denoted 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A). For scoring purposes card values are ordered as above, with 2 having the lowest and ace the highest value. The suit has no impact on value.

A poker hand consists of five cards dealt from the deck. Poker hands are ranked by the following partial order from lowest to highest.

(1)High Card:
Hands which do not fit any higher category are ranked by the value of their highest card. If the highest cards have the same value, the hands are ranked by the next highest, and so on.For example:8C 9D 2H 3S 4H
(2)Pair:
Two of the five cards in the hand have the same value. Hands which both contain a pair are ranked by the value of the cards forming the pair. If these values are the same, the hands are ranked by the values of the cards not forming the pair, in decreasing order.For example:8C 8D 2H 3S 4H
(3)Two Pairs:
The hand contains two different pairs. Hands which both contain two pairs are ranked by the value of their highest pair. Hands with the same highest pair are ranked by the value of their other pair. If these values are the same the hands are ranked by the value of the remaining card.For example:8C 8D 2H 2S 4H
(4)Three of a Kind:
Three of the cards in the hand have the same value. Hands which both contain three of a kind are ranked by the value of the three cards.For example:8C 8D 8H 3S 4H
(5)Straight:
Hand contains five cards with consecutive values. Hands which both contain a straight are ranked by their highest card.For example:2C 3D 4H 5S 6H
(6)Flush:
Hand contains five cards of the same suit. Hands which are both flushes are ranked using the rules for High Card.For example:8C 9C 2C 3C 4C
(7)Full House:
Three cards of the same value, with the remaining two cards forming a pair. Ranked by the value of the three cards.For example:8C 8D 8H 3S 3H
(8)Four of a Kind:
Four cards with the same value. Ranked by the value of the four cards.For example:8C 8D 8H 8S 4H
(9)Straight Flush:
Five cards of the same suit with consecutive values. Ranked by the highest card in the hand.For example:2C 3C 4C 5C 6C
Your job is to compare several pairs of poker hands and to indicate which, if either, has a higher rank.

输入

The input file contains several lines, each containing the designation of ten cards: the first five cards are the hand for the player named "Black" and the next five cards are the hand for the player named "White".

输出

For each line of input, print a line containing one of the following:

Black wins.
White wins.
Tie.

样例输入

2H 3D 5S 9C KD 2C 3H 4S 8C AH
2H 4S 4C 2D 4H 2S 8S AS QS 3S
2H 3D 5S 9C KD 2C 3H 4S 8C KH
2H 3D 5S 9C KD 2D 3H 5C 9S KH

样例输出

White wins.
Black wins.
Black wins.
Tie.

这道题其实就是梭哈,但是经过我测试发现其中有一个人(Black或White)手里5张牌都是
一样的情况,譬如Black手里有5个2.....不得不吐槽是个Bug啊....由于本人英文没有那
么好,所以当问老师这个BUG时候他说要求里说了5个一样的就看成四条带一个...
接下来上代码,我写的比较繁琐,写的差不多了才想到用结构体,懒得改了,如果有朋友
能耐心看这个代码可以改成结构体


上代码了~


#include <stdio.h>
#include<string.h>
#define MAXN_P 29+1
#define MAXN_N 10 + 1
#define MAXN_A 5
#define MAXN_WH 4
char pok[MAXN_P];//全部扑克
/*例如:
8H 7H 6H 5H 4H
a[]={4,5,6,7,8}//a是自己手牌除了从小到大排序没做别的变化
b[]={4,5,6,7,8}//b是在a基础上把手牌一样的重合只显示一个例如8,8只显示一个8,其余的从0开始用0站位,同时按d的顺序进行一一对应排列
c[]={0,0,5,0}//c是CDHS四个花色的个数
d[]={1,1,1,1,1}//d是与b一一对应,显示b的每个对应位置的牌的个数
再例:
8H 9H 7H 8C 7C
a[]={7,7,8,8,9}
b[]={0,0,7,8,9}
c[]={2,0,3,0}
d[]={0,0,2,2,1}
*/
void score(int a[], int b[],int c[],int d[], int *s)
{
    memset(d,0,5*sizeof(int));
    memset(b,0,5*sizeof(int));
    int i, j, temp;
    j = 3;
    b[4] = a[4];
    for(i = 3; i >= 0; i--){//b把a中相同数字重合然后小到大排序前面用0站位,生成b
        if(a[i] != a[i+1]){
            b[j] = a[i];
            j--;
        }
    }
    //for(i = 0; i < 5; i++)printf("%d ", b[i]);
    j = 4;
    d[4] = 1;
    for(i = 3; i >= 0; i--){// d:与b一一对应关系,为b中对应位置的数字个数
        if(a[i] == a[i+1]){
            d[j]++;
            continue;
        }
        j--;
        d[j]++;
    }
    for(i = 0; i < 4; i++){
        for(j = i+1; j < 5; j++){//按d排序小到大,顺便把b顺序也按d变了,同时如果两个d相等则两者同时按对应位置b的大小进行互换
            if(d[i] > d[j]){
                temp = d[i];
                d[i] = d[j];
                d[j] = temp;
                temp = b[i];
                b[i] = b[j];
                b[j] = temp;
            }
            else if(d[i] == d[j]){
                if(b[i]>b[j]){
                    temp = b[i];
                    b[i] = b[j];
                    b[j] = temp;
                }
            }
        }
    }
    /*这步(按d排序小到大,顺便把b顺序也按d变了)是如例子2变成了
    8H 9H 7H 8C 7C
    a[]={7,7,8,8,9}
    b[]={0,0,7,8,9}
    c[]={2,0,3,0}
    d[]={0,0,2,2,1}
    b'[]={0,0,9,7,8}
    d'[]={0,0,1,2,2}
    */
    if(d[0] ==1&& d[1] ==1&& d[2] ==1&& d[3] ==1&& d[4] ==1){
        if(b[4]-b[3]==1&&b[3]-b[2]==1&&b[2]-b[1]==1&&b[1]-b[0]==1&&(c[3] == 5||c[0] == 5 || c[1]==5 ||c[2]==5)) *s = 9;
        else if(b[4]-b[3]==1&&b[3]-b[2]==1&&b[2]-b[1]==1&&b[1]-b[0]==1&&c[3]!=5)    *s = 5;
        else if(c[3] == 5||c[0] == 5 || c[1]==5 ||c[2]==5)  *s = 6;
        else   
            *s = 1;
    }
    else if(d[4] == 2 && d[3] == 1) *s = 2;
    else if(d[4] == 2 && d[3] == 2) {
        *s = 3;
    }
    else if(d[4] == 3 && d[3]==1)   *s = 4;
    //else if(c[3] == 5)    *s = 6;
    else if(d[4] == 3 && d[3] == 2) *s = 7;
    else if(d[4] == 4||d[4]==5) *s = 8;
    //分数确定完毕
 
}
void main()
{
    int i, j, s_b, s_w, temp;
    while(gets(pok) != NULL){
        j = 0;
        int wc[MAXN_A] = {0};
        int bc[MAXN_A] = {0};
        int ge_w[MAXN_A] = {0};
        int ge_b[MAXN_A] = {0};
        int pok_n[MAXN_A] = {0};//每相同张牌的个数,从大到小排序
        int bh[MAXN_WH] = {0};// black 的花色
        int wh[MAXN_WH] = {0};// white 的花色
        int b[MAXN_A] = {0};//black 5张的牌
        int w[MAXN_A] = {0};//white 5张的牌
        for(i = 0; i < 13; i += 3){//black 5张的牌
            if(pok[i] >= 50 && pok[i] <= 57)
                b[j] = pok[i] - 48;
            else if(pok[i] == 'T')  b[j] = 10;
            else if(pok[i] == 'J')  b[j] = 11;
            else if(pok[i] == 'Q')  b[j] = 12;
            else if(pok[i] == 'K')  b[j] = 13;
            else if(pok[i] == 'A')  b[j] = 14;
            j++;
        }
 
        j = 0;
 
        for(i = 15; i < 29; i += 3){//white 5张牌
            if(pok[i] >= 50 && pok[i] <= 57)
                w[j] = pok[i] - 48;
            else if(pok[i] == 'T')  w[j] = 10;
            else if(pok[i] == 'J')  w[j] = 11;
            else if(pok[i] == 'Q')  w[j] = 12;
            else if(pok[i] == 'K')  w[j] = 13;
            else if(pok[i] == 'A')  w[j] = 14;
            j++;
        }
 
        for(i = 1; i < 14; i += 3){// black 的花色
            if(pok[i] == 'C')   bh[0]++;
            else if(pok[i] == 'D')  bh[1]++;
            else if(pok[i] == 'H')  bh[2]++;
            else if(pok[i] == 'S')  bh[3]++;
        }
        for(i = 16; i < 29; i += 3){// white 的花色
            if(pok[i] == 'C')   wh[0]++;
            else if(pok[i] == 'D')  wh[1]++;
            else if(pok[i] == 'H')  wh[2]++;
            else if(pok[i] == 'S')  wh[3]++;
        }
        for(i = 0; i < 4; i++){//给b  w  的牌按从小到大排序
            for(j = i+1; j < 5; j++){
                if(b[i] > b[j]){
                    temp = b[i];
                    b[i] = b[j];
                    b[j] = temp;
                }
                if(w[i] > w[j]){
                    temp = w[i];
                    w[i] = w[j];
                    w[j] = temp;
                }
            }
        }
        //for(i = 0; i < 5; i++)printf("%d\n", b[i]);
        score(b, bc, bh, ge_b, &s_b);
        score(w, wc, wh, ge_w, &s_w);
        //printf("%d\n%d\n",sco_b,sco_w);
        if(s_b > s_w)
            printf("Black wins.\n");
        if(s_w > s_b)
            printf("White wins.\n");
        if(s_b == s_w){
            for(i = 4; i >=0; i--){
                //printf("%d %d\n", bc[i], wc[i]);
                if(bc[i] > wc[i]){
                    printf("Black wins.\n");
                    break;
                }
                if(bc[i] < wc[i]){
                    printf("White wins.\n");
                    break;
                }
            }
        }
 
        if(i == -1) printf("Tie.\n");
        /*for(i = 0;i<5;i++)printf("%d ", b[i]);printf("\n");
        for(i = 0;i<5;i++)printf("%d ", w[i]);printf("\n");
        for(i = 0;i<5;i++)printf("%d ", bc[i]);printf("\n");
        for(i = 0;i<5;i++)printf("%d ", wc[i]);printf("\n");
        for(i = 0;i<4;i++)printf("%d ", bh[i]);printf("\n");
        for(i = 0;i<4;i++)printf("%d ", wh[i]);printf("\n");
        for(i = 0;i<5;i++)printf("%d ", ge_b[i]);printf("\n");
        for(i = 0;i<5;i++)printf("%d ", ge_w[i]);printf("\n");
        printf("%d %d\n",s_b,s_w);*/
    }
}
 
/**************************************************************
    Problem: 2176
    User: 2012014425
    Language: C
    Result: 正确
    Time:16 ms
    Memory:768 kb
****************************************************************/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值