判断5张扑克牌分析其组成

来源:互联网 发布:淘宝上能买到真阿胶吗 编辑:程序博客网 时间:2024/05/13 06:44

脑洞大开:

一副牌中发五张扑克牌给你,让你判断数字的组成:

有以下几种情况:
1:四条:即四张一样数值的牌(牌均不论花色)
2:三条带一对
3:三条带两张不相同数值的牌
4:两对
5:顺子(包括10,J,Q,K,A)
6:什么都不是
7:只有一对 


分析:


应该不包含大小王,另外J、Q、K、A分别相当于11、12、13、14,虽然从2到A一共是13张牌,但是为了方便对应,使用了一个包含15个元素的数组来记录每一个点数出现的次数(舍弃0号元素和1号元素)。


为了记录发给你的5张牌,直接用整型肯定不行,不能接受J、Q、K、A的输入,刚开始的时候准备使用一个字符数组接受输入,然后再转换为整型,但是字符输入是不能接受10这样的输入的,最后只能使用字符串数组接受输入。
用扑克分析仪分析的时候写码:长春扑克分析仪:l5lo444o777   qq:ll3882l689【长春火车站】

C代码如下:


[cpp] view plaincopy
#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
  
#define LEN 5  
#define TOTAL_LEN 15   
  
char str_poker[LEN][3];  
int int_poker[LEN] = {0};  
int total_poker[TOTAL_LEN] = {0};  
  
void check(int len, int poker[]);  
void ch_to_int(int len, char str_array[][3], int int_array[]);  
void int_sort(int len, int a[]);  
  
int main()  
{  
    int i;  
    printf("please input your cards:");  
    for(i=0; i<LEN; i++)  
        scanf("%s",str_poker[i]);  
    ch_to_int(LEN, str_poker, int_poker);  
    printf("the result is:");  
    check(LEN, int_poker);  
    return 0;  
}  
  
void ch_to_int(int len, char str_array[][3], int int_array[])  
{  
    int i;  
    for(i=0; i<LEN; i++)  
    {  
        if(strcmp(str_array[i],"10") == 0)  
        {  
            int_array[i] = 10;  
            continue;  
        }  
        if(strcmp(str_array[i],"J") == 0)  
        {  
            int_array[i] = 11;  
            continue;  
        }  
        if(strcmp(str_array[i],"Q") == 0)  
        {  
            int_array[i] = 12;  
            continue;  
        }  
        if(strcmp(str_array[i],"K") == 0)  
        {  
            int_array[i] = 13;  
            continue;  
        }  
        if(strcmp(str_array[i],"A") == 0)  
        {  
            int_array[i] = 14;  
            continue;  
        }  
        int_array[i] = atoi(str_array[i]);    
    }  
}  
  
void int_sort(int len, int a[])  
{  
    int i,j,temp;  
    for(i=0; i<len-1; i++)  
        for(j=0; j<len-1; j++)  
            if(a[j] > a[j+1])  
            {  
                temp = a[j];  
                a[j] = a[j+1];  
                a[j+1] = temp;  
            }  
}  
  
void check(int len, int poker[])  
{  
    int i;  
    int res = 1;  
    for(i=0; i<LEN; i++)  
        total_poker[poker[i]]++;  
    for(i=1; i<=TOTAL_LEN; i++)  
    {  
        if(total_poker[i] == 4)  
        {  
            printf("四条加一张\n");  
            return;  
        }  
        if(total_poker[i] >= 2)  
            res *= total_poker[i];  
    }  
    if(res > 1)  
    {  
        switch(res)  
        {  
        case 2:  
            printf("一对加三张不同\n");  
            break;  
        case 3:  
            printf("三条加两张不同\n");  
            break;  
        case 4:  
            printf("两队加一张\n");  
            break;  
        case 6:  
            printf("三条加一对\n");  
            break;  
        }  
    }  
    else  
    {  
        int_sort(LEN, poker);  
        for(i=0; i<LEN-1; i++)  
        {  
            if(poker[i+1] != poker[i] + 1)  
            {  
                printf("什么都不是\n");  
                return;  
            }  
        }  
        printf("五张顺子\n");  
    }  

}  




0 0