题解 | #扑克牌大小#

扑克牌大小

http://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb

#include <stdio.h>
#include <string.h>

int cmpab(char *a, char *b);
int main()
{
    char str[50];
   while(fgets(str,50, stdin)!=NULL){
        int alen = 0;
        int blen = 0; //统计空格
        char a[50] = {'\0'}, b[50] = {'\0'}, max[50] = {'\0'};
        int insert = 0;
        while (str[insert] != '-')
            insert++;
        for (int i = 0; i < insert; i++)
        {
            a[i] = str[i];
            if (str[i] == ' ')
                alen++;
        }
        for (int j = insert + 1; j < strlen(str)-1; j++)
        {
            b[j - insert - 1] = str[j];
            if (str[j] == ' ')
                blen++;
        }
        //获取第一个值
        char af[20] = {'\0'}, bf[20] = {'\0'};
        char as[20] = {'\0'}, bs[20] = {'\0'};
        sscanf(a, "%s %s", af, as);
        sscanf(b, "%s %s", bf, bs);

        if (strstr(a, "joker JOKER") != NULL || strstr(b, "joker JOKER") != NULL)
        {
            strcpy(max, "joker JOKER\n");
        }                                   
        else if (alen == 3 || blen == 3)//四张炸弹
        {
            if (alen == 3 && blen == 3)
            {

                if (cmpab(af, bf) > 0)
                    strcpy(max, a);
                else
                    strcpy(max, b);
            }
            else if (alen == 3)
            {
                strcpy(max, a);
            }
            else if (blen == 3)
            {
                strcpy(max, b);
            }
        }
        else if (alen == blen)
        {
            if (alen == 4)
            {
                if (cmpab(as, bs) > 0)
                    strcpy(max, a);
                else
                    strcpy(max, b);
            }
            else if (alen == 2 || alen == 1 || alen == 0)
            {
                if (cmpab(af, bf) > 0)
                    strcpy(max, a);
                else
                    strcpy(max, b);
            }
        }
        else if (alen != blen)
        {
            strcpy(max, "ERROR\n");
        }

   printf("%s\n", max);  


    
   }
    return 0;
}

int cmpab(char *a, char *b)
{
    int ret;

    int anum = a[0] - '0', bnum = b[0] - '0';
    if (a[0] == 'J')
        anum = 11;
         if (a[0] == 'j')
        anum = 10;
    else if (a[0] == 'Q')
        anum = 12;
    else if (a[0] == 'K')
        anum = 13;
    else if (a[0] == 'A')
        anum = 14;
    else if (a[0] == '2')
        anum = 15;

    if (b[0] == 'J')
        bnum = 11;
     if (b[0] == 'j')
        bnum = 10;
    else if (b[0] == 'Q')
        bnum = 12;
    else if (b[0] == 'K')
        bnum = 13;
    else if (b[0] == 'A')
        bnum = 14;
    else if (b[0] == '2')
        bnum = 15;

    if (strlen(a) == 2)
        anum = 10;
    if (strlen(b) == 2)
        bnum = 10;
        if (strlen(a) == 5)
        anum += 10;
    if (strlen(b) == 5)
        bnum += 10;

    ret = anum - bnum;
    return ret;
}

全部评论

相关推荐

评论
点赞
1
分享
牛客网
牛客企业服务