题解 | #扑克牌大小#

扑克牌大小

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;
}

全部评论

相关推荐

努力学习的小绵羊:我反倒觉得这种挺好的,给不到我想要的就别浪费大家时间了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
10-12 10:48
已编辑
秋招之苟:邻居家老哥19届双2硕大厂开发offer拿遍了,前几天向他请教秋招,他给我看他当年的简历,0实习实验室项目技术栈跟开发基本不沾边😂,我跟他说这个放在现在中厂简历都过不了
点赞 评论 收藏
分享
点赞 1 评论
分享
牛客网
牛客企业服务