题目很长,但就是一道模拟题,只是有许多细节要注意,考虑不全wa了好多次。对我来说,最关键的错误是:当牌型相同时,比较的是题目要求的相应牌的大小,开始没看清,导致都是从最大牌比到最小的一张。加上这个判断后,还要在写判断每种牌型时细致一些。注意到这些也就没什么了。我写的比较直接,所以代码比较长.
以下是我的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char poker[50];//储存数据
int b[16],w[16],compb[6],compw[6];//b,w分别是记录黑方和白方每种牌出现的次数,comb和comw是存两人的牌(只存数字)
int vb,vw;//记录两人牌型(牌型从小到大分别用1至9表示)
int compler(const void*a,const void*b)
{
    return *(int *)b-*(int *)a;
}
void order()//判断牌型
{
    int i,j,k,k1,flag,two=0,three=0,four=0;
    vb=vw=1;
    if(poker[1]==poker[4]&&poker[4]==poker[7]&&poker[7]==poker[10]&&poker[10]==poker[13])
        vb=6;
    if(poker[16]==poker[19]&&poker[19]==poker[22]&&poker[22]==poker[25]&&poker[25]==poker[28])
        vw=6;//看两人牌是否是同花
    memset(b,0,sizeof(b));
    memset(w,0,sizeof(w));
    memset(compb,0,sizeof(compb));
    memset(compw,0,sizeof(compw));
    for(i=0; i<13; i+=3)
    {
        if(poker[i]>='2'&&poker[i]<='9')
            b[poker[i]-'1'+1]++;
        else if(poker[i]=='T')
            b[10]++;
        else if(poker[i]=='J')
            b[11]++;
        else if(poker[i]=='Q')
            b[12]++;
        else if(poker[i]=='K')
            b[13]++;
        else if(poker[i]=='A')
            b[14]++;
    }//记录黑方每种牌出现次数
    for(i=15; i<28; i+=3)
    {
        if(poker[i]>='2'&&poker[i]<='9')
            w[poker[i]-'1'+1]++;
        else if(poker[i]=='T')
            w[10]++;
        else if(poker[i]=='J')
            w[11]++;
        else if(poker[i]=='Q')
            w[12]++;
        else if(poker[i]=='K')
            w[13]++;
        else if(poker[i]=='A')
            w[14]++;
    }//记录白方每种牌出现次数
    k=k1=0;
    for(i=14;i>=2;i--)
    {
        for(j=0;j<b[i];j++)
            compb[k++]=i;
        for(j=0;j<w[i];j++)
            compw[k1++]=i;
    }//从最大牌开始一每种牌出现次数往comb和comw里写数(A用14表示)
    //以下先判断黑方
    for(i=0; i<5; i++)
    {
        k=1;
        for(j=i+1; j<5; j++)
        {
            if(compb[j]==compb[i])
                k++;
            else
            {
                i=j-1;
                break;
            }
        }//看当前牌中每种牌出现次数(这里写的比较啰嗦,因为是先写的这个,改进后直接拿来用就没再细想,应该可以和上一步做规整的)
        
        if(k==2)
            two++;
        if(k==3)
            three++;
        if(k==4)
            four++;//two,three,four分别表示该张牌出现2,3,4,次
        if(j==5)
            break;
    }
    if(four==1)
        vb=8;//出现4张即为“四张”(以下情况均对应每种牌)
    else if(two==1&&three==1)
        vb=7;
    if(vb==1)
    {
        if(three==1)
            vb=4;
        else if(two==2)
            vb=3;
        else if(two==1)
            vb=2;
    }
    flag=0;
    for(i=0; i<4; i++)
    {
        if(compb[i]-compb[i+1]!=1)
        {
            flag=1;
            break;
        }
    }//看是否是顺子
    if(flag==0&&vb<5)
        vb=5;//是顺子又不是同花为“顺子”,否则为“同花顺”
    else if(flag==0&&vb==6)
        vb=9;
    two=three=four=0;
    //判断白方与上一步相同
 
    for(i=0; i<5; i++)
    {
        k=1;
        for(j=i+1; j<5; j++)
        {
            if(compw[j]==compw[i])
                k++;
            else
            {
                i=j-1;
                break;
            }
        }
        
        if(k==2)
            two++;
        if(k==3)
            three++;
        if(k==4)
            four++;
        if(j==5)
            break;
    }
    if(four==1)
        vw=8;
    else if(two==1&&three==1)
        vw=7;
    if(vw==1)
    {
        if(three==1)
            vw=4;
        else if(two==2)
            vw=3;
        else if(two==1)
            vw=2;
    }
    flag=0;
    for(i=0; i<4; i++)
    {
        if(compw[i]-compw[i+1]!=1)
        {
            flag=1;
            break;
        }
    }
    if(flag==0&&vw<5)
        vw=5;
    else if(flag==0&&vw==6)
        vw=9;
}
void maxorder()//判断若牌型相同,该比较的顺序(我这里均是在comb与comw中操作,即改变两数组的顺序,所以最后不管怎样都通过这两个数组比就可以)
{
    int i,j,k,t;
    if(vb==8)
    {
        if(compb[0]<compb[1]||compb[4]>compb[3])
        {   t=compb[0];compb[0]=compb[4];compb[4]=t;}
    }
    if(vb==7)
    {
        if(compb[1]!=compb[2])
        {
            t=compb[0];compb[0]=compb[4];compb[4]=t;
            t=compb[1];compb[1]=compb[3];compb[3]=t;
        }
    }
    if(vb==4)
    {
        if(compb[0]!=compb[1]&&compb[1]!=compb[2])
        {
            t=compb[0];compb[0]=compb[3];compb[3]=t;
            t=compb[1];compb[1]=compb[4];compb[4]=t;
        }
        else if(compb[0]!=compb[1]&&compb[1]==compb[2])
        {   t=compb[0];compb[0]=compb[3];compb[3]=t;}
    }
    if(vb==3)
    {
        if(compb[0]!=compb[1])
        {
            t=compb[0];
            for(i=1;i<5;i++)
                compb[i-1]=compb[i];
            compb[4]=t;
        }
        else if(compb[2]!=compb[3])
        {   t=compb[2];compb[2]=compb[4];compb[4]=t;}
    }
    if(vb==2)
    {
        if(compb[1]==compb[2])
        {   t=compb[2];compb[2]=compb[0];compb[0]=t;}
        else if(compb[3]==compb[2])
        {
            t=compb[2];compb[2]=compb[0];compb[0]=t;
            t=compb[3];compb[3]=compb[1];compb[1]=t;
        }
        else if(compb[3]==compb[4])
        {
            t=compb[3];compb[3]=compb[1];compb[1]=t;
            t=compb[4];compb[4]=compb[0];compb[0]=t;
            t=compb[2];compb[2]=compb[4];compb[4]=t;
        }
    }
 
 
    if(vw==8)
    {
        if(compw[0]<compw[1]||compw[4]>compw[3])
        {   t=compw[0];compw[0]=compw[4];compw[4]=t;}
    }
    if(vw==7)
    {
        if(compw[1]!=compw[2])
        {
            t=compw[0];compw[0]=compw[4];compw[4]=t;
            t=compw[1];compw[1]=compw[3];compw[3]=t;
        }
    }
    if(vw==4)
    {
        if(compw[0]!=compw[1]&&compw[1]!=compw[2])
        {
            t=compw[0];compw[0]=compw[3];compw[3]=t;
            t=compw[1];compw[1]=compw[4];compw[4]=t;
        }
        else if(compw[0]!=compw[1]&&compw[1]==compw[2])
        {   t=compw[0];compw[0]=compw[3];compw[3]=t;}
    }
    if(vw==3)
    {
        if(compw[0]!=compw[1])
        {
            t=compw[0];
            for(i=1;i<5;i++)
                compw[i-1]=compw[i];
            compw[4]=t;
        }
        else if(compw[2]!=compw[3])
        {   t=compw[2];compw[2]=compw[4];compw[4]=t;}
    }
    if(vw==2)
    {
        if(compw[1]==compw[2])
        {   t=compw[2];compw[2]=compw[0];compw[0]=t;}
        else if(compw[3]==compw[2])
        {
            t=compw[2];compw[2]=compw[0];compw[0]=t;
            t=compw[3];compw[3]=compw[1];compw[1]=t;
        }
        else if(compw[3]==compw[4])
        {
            t=compw[3];compw[3]=compw[1];compw[1]=t;
            t=compw[4];compw[4]=compw[0];compw[0]=t;
            t=compw[2];compw[2]=compw[4];compw[4]=t;
        }
    }
 
}
 
int main()
{
    int i;
    while(gets(poker))
    {
        order();
        maxorder();
        if(vb>vw)
            printf("Black wins.\n");
        else if(vb<vw)
            printf("White wins.\n");
        else
        {
            for(i=0; i<5; i++)
            {
                if(compb[i]!=compw[i])
                {
                    if(compb[i]>compw[i])
                        printf("Black wins.\n");
                    else
                        printf("White wins.\n");
                    break;
                }
            }
            if(i==5)
                printf("Tie.\n");
        }
    }
    return 0;
}
 
/**************************************************************
    Problem: 1023
    User: jk11171207
    Language: C
    Result: Accepted
    Time:0 ms
    Memory:840 kb
****************************************************************/

 

发表评论

电子邮件地址不会被公开。

Post Navigation