zoukankan      html  css  js  c++  java
  • UVA 131 The Psychic Poker Player

    此题写起来有点烦躁,但思路并不难。枚举一下丢1,2,3,4,5张的所有情况,就可以出答案了。DFS。

    #include<stdio.h>
    char s1[6][3], s2[6][3];//输入
    int hand[6], deck[6];//输入点数的转换成数字
    int flag[6];//标记这张牌用不用 1表示用
    int ff[6];//保存当前枚举到的牌的点数
    char pp[6];//保存当前枚举到的牌的点数
    int max = 0;//求出最大值
    int k = 0;//当前枚举到的情况
    void straightflush()
    {
        if (pp[1] == pp[2] && pp[1] == pp[3] && pp[1] == pp[4] && pp[1] == pp[5])
        {
            if ((ff[1] + 1 == ff[2] && ff[2] + 1 == ff[3] && ff[3] + 1 == ff[4] && ff[4] + 1 == ff[5]
                ) || (ff[1] == 1 && ff[2] == 10 && ff[3] == 11 && ff[4] == 12 && ff[5] == 13))
                k = 8;
        }
        return;
    }
    void fourofakind()
    {
        if ((ff[1] == ff[2] && ff[1] == ff[3] && ff[1] == ff[4])
            || (ff[2] == ff[3] && ff[2] == ff[4] && ff[2] == ff[5]))
            k = 7;
        return;
    }
    void fullhouse()
    {
        if ((ff[1] == ff[2] && ff[1] == ff[3] && ff[4] == ff[5])
            || (ff[1] == ff[2] && ff[3] == ff[4] && ff[3] == ff[5]))
            k = 6;
        return;
    }
    void flush()
    {
        if (pp[1] == pp[2] && pp[1] == pp[3] && pp[1] == pp[4] && pp[1] == pp[5])
            k = 5;
        return;
    }
    void straight()
    {
        if ((ff[1] + 1 == ff[2] && ff[2] + 1 == ff[3] && ff[3] + 1 == ff[4] && ff[4] + 1 == ff[5]
            ) || (ff[1] == 1 && ff[2] == 10 && ff[3] == 11 && ff[4] == 12 && ff[5] == 13))
            k = 4;
        return;
    }
    void threeofakind()
    {
        if ((ff[1] == ff[2] && ff[1] == ff[3]) || (ff[2] == ff[3] && ff[2] == ff[4]) || (ff[3] == ff[4] && ff[3] == ff[5]))
            k = 3;
        return;
    }
    void twopairs()
    {
        if ((ff[1] == ff[2] && ff[4] == ff[5]) || (ff[2] == ff[3] && ff[4] == ff[5]) || (ff[1] == ff[2] && ff[3] == ff[4]))
            k = 2;
        return;
    }
    void onepair()
    {
        if (ff[1] == ff[2] || ff[2] == ff[3] || ff[3] == ff[4] || ff[4] == ff[5])
        k = 1;
        return;
    }
    void panduan(int ans)
    {
        int i, j, r = 1, t, s = 8;
        char g;
        if (ans == 6)
        {
            //下面构造出ff和pp数组
            for (i = 1; i <= 5; i++)
            {if (flag[i] == 1){ ff[r] = hand[i]; pp[r] = s1[i][1]; r++; }}
            i = 1;
            while (r < 6){ ff[r] = deck[i]; pp[r] = s2[i][1]; r++; i++; }
            for (j = 1; j < 6; j++)
            {
                for (i = 1; i<6  - j; i++)
                {
                    if (ff[i]>ff[i + 1])
                    {
                        t = ff[i];
                        ff[i] = ff[i + 1];
                        ff[i + 1] = t;
                        g = pp[i];
                        pp[i] = pp[i + 1];
                        pp[i + 1] = g;
                    }
                }
            }
            if (k == 0)straightflush();
            if (k == 0)fourofakind();
            if (k == 0)fullhouse();
            if (k == 0)flush();
            if (k == 0)straight();
            if (k == 0)threeofakind();
            if (k == 0)twopairs();
            if (k == 0)onepair();
            /*这里写8个判断函数*/
    
            if (k >= max) max = k;
            for (i = 1; i <= 5; i++) { ff[i] = 0; pp[i] = ''; }
            k = 0; return;
        }
        flag[ans] = 1;
        panduan(ans + 1);
        flag[ans] = 0;
        panduan(ans + 1);
    }
    int main()
    {
        int i;
        while (~scanf("%s", s1[1]))
        {
            max = 0; k = 0;
            for (i = 2; i <= 5; i++) scanf("%s", s1[i]);
            for (i = 1; i <= 5; i++) scanf("%s", s2[i]);
            for (i = 1; i <= 5; i++) flag[i] = 0; max = 0;
            for (i = 1; i <= 5; i++)
            {
                if (s1[i][0] == 'A') hand[i] = 1;
                else if (s1[i][0] >= '2' && s1[i][0] <= '9') hand[i] = s1[i][0] - '0';
                else if (s1[i][0] == 'T') hand[i] = 10;
                else if (s1[i][0] == 'J') hand[i] = 11;
                else if (s1[i][0] == 'Q') hand[i] = 12;
                else if (s1[i][0] == 'K') hand[i] = 13;
            }
            for (i = 1; i <= 5; i++)
            {
                if (s2[i][0] == 'A') deck[i] = 1;
                else if (s2[i][0] >= '2' && s2[i][0] <= '9') deck[i] = s2[i][0] - '0';
                else if (s2[i][0] == 'T') deck[i] = 10;
                else if (s2[i][0] == 'J') deck[i] = 11;
                else if (s2[i][0] == 'Q') deck[i] = 12;
                else if (s2[i][0] == 'K') deck[i] = 13;
            }
            panduan(1);
            printf("Hand: ");
            printf("%s %s %s %s %s ", s1[1], s1[2], s1[3], s1[4], s1[5]);
            printf("Deck: ");
            printf("%s %s %s %s %s ", s2[1], s2[2], s2[3], s2[4], s2[5]);
            printf("Best hand: ");
            if (max == 0)printf("highest-card
    ");
            if (max == 1)printf("one-pair
    ");
            if (max == 2)printf("two-pairs
    ");
            if (max == 3)printf("three-of-a-kind
    ");
            if (max == 4)printf("straight
    ");
            if (max == 5)printf("flush
    ");
            if (max == 6)printf("full-house
    ");
            if (max == 7)printf("four-of-a-kind
    ");
            if (max == 8)printf("straight-flush
    ");
        }
        return 0;
    }
  • 相关阅读:
    如何删除完全重复的列
    串联多个字符串,引发string和stringBuilder的比较
    借鉴ANJOU的方法改写了上次的TreeView
    如何改变ListBox中内容的顺序
    执行Insert语句时使用string的Format用法
    moss 2007 中FCKEditor编辑器的使用
    qt 获得cmd 命令运行的结果 GIS
    迭代器 GIS
    win32 创建带图片的button GIS
    c++标准库的构成 GIS
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4430911.html
Copyright © 2011-2022 走看看