#include
struct Poker
{
char num;
char color;
};
int comparenum(char a,char b)
{
a=(a=='A'?'9'+5:a);
b=(b=='A'?'9'+5:b);
a=(a=='K'?'9'+4:a);
b=(b=='K'?'9'+4:b);
a=(a=='Q'?'9'+3:a);
b=(b=='Q'?'9'+3:b);
a=(a=='J'?'9'+2:a);
b=(b=='J'?'9'+2:b);
a=(a=='1'?'9'+1:a);
b=(b=='1'?'9'+1:b);
return int(a-b);
}
int comparecolor(char a,char b)
{
switch(a)
{
case 'C':if(b=='D'||b=='S'||b=='H')return -1;
else return 0;
case 'D':if(b=='S'||b=='H')return -1;
else if(b=='C')return 1;
else return 0;
case 'S':if(b=='H')return -1;
else if(b=='C'||b=='D')return 1;
else return 0;
case 'H':if(b=='C'||b=='D'||b=='S')return 1;
else return 0;
default:break;
}
return 0;
}
void sortpoker(Poker p[3])
{
Poker temp;
int i,j;
for(i=0;i<3;i++)
{
for(j=i;j<3;j++)
{
if(comparenum(p[i].num,p[j].num)<0)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
for(i=0;i<3;i++)
{
for(j=i;j<3;j++)
{
if(comparenum(p[i].num,p[j].num)==0&&comparecolor(p[i].color,p[j].color)<0)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
}
int isline(Poker p[3])
{
if(comparenum(p[1].num,p[0].num)==-1&&comparenum(p[2].num,p[1].num)==-1)
return 1;
else
return 0;
}
int isflush(Poker p[3])
{
if(comparecolor(p[0].color,p[1].color)==0&&comparecolor(p[1].color,p[2].color)==0\
&&isline(p))
return 1;
else
return 0;
}
int isbomb(Poker p[3])
{
if(comparenum(p[0].num,p[2].num)==0)
return 1;
else
return 0;
}
int isdouble(Poker p[3])
{
if(comparenum(p[0].num,p[1].num)==0||comparenum(p[0].num,p[2].num)==0\
||comparenum(p[1].num,p[2].num)==0)
return 1;
else
return 0;
}
int diffpoker(Poker p[3])
{
if(p[0].num==p[1].num)
return 2;
else
return 0;
}
int comparepoker(Poker p1[3],Poker p2[3])
{
int index1;
int index2;
if(isflush(p1))
{
if(isflush(p2))
{
if(comparenum(p1[0].num,p2[0].num)>0)
return 1;
else if(comparenum(p1[0].num,p2[0].num)<0)
return -1;
else if(comparecolor(p1[0].color,p2[0].color)>0)
return 1;
else if(comparecolor(p1[0].color,p2[0].color)<0)
return -1;
else if(comparecolor(p1[1].color,p2[1].color)>0)
return 1;
else if(comparecolor(p1[1].color,p2[1].color)<0)
return -1;
else if(comparecolor(p1[2].color,p2[2].color)>0)
return 1;
else if(comparecolor(p1[2].color,p2[2].color)<0)
return -1;
else
return 0;
}
else
return 1;
}
else if(isbomb(p1))
{
if(isflush(p2))
return -1;
else if(isbomb(p2))
{
if(comparenum(p1[0].num,p2[0].num)>0)
return 1;
else if(comparenum(p1[0].num,p2[0].num)<0)
return -1;
else if(comparecolor(p1[0].color,p2[0].color)>0)
return 1;
else if(comparecolor(p1[0].color,p2[0].color)<0)
return -1;
else if(comparecolor(p1[1].color,p2[1].color)>0)
return 1;
else if(comparecolor(p1[1].color,p2[1].color)<0)
return -1;
else if(comparecolor(p1[2].color,p2[2].color)>0)
return 1;
else if(comparecolor(p1[2].color,p2[2].color)<0)
return -1;
else
return 0;
}
else
return 1;
}
else if(isline(p1))
{
if(isflush(p2)||isbomb(p2))
return -1;
else if(isline(p2))
{
if(comparenum(p1[0].num,p2[0].num)>0)
return 1;
else if(comparenum(p1[0].num,p2[0].num)<0)
return -1;
else if(comparecolor(p1[0].color,p2[0].color)>0)
return 1;
else if(comparecolor(p1[0].color,p2[0].color)<0)
return -1;
else if(comparecolor(p1[1].color,p2[1].color)>0)
return 1;
else if(comparecolor(p1[1].color,p2[1].color)<0)
return -1;
else if(comparecolor(p1[2].color,p2[2].color)>0)
return 1;
else if(comparecolor(p1[2].color,p2[2].color)<0)
return -1;
else
return 0;
}
else
return 1;
}
else if(isdouble(p1))
{
if(isflush(p2)||isbomb(p2)||isline(p2))
return -1;
else if(isdouble(p2))
{
if(comparenum(p1[1].num,p2[1].num)>0)
return 1;
else if(comparenum(p1[1].num,p2[1].num)<0)
return -1;
else if(comparenum(p1[index1=diffpoker(p1)].num,p2[index2=diffpoker(p2)].num)>0)
return 1;
else if(comparenum(p1[index1].num,p2[index2].num)<0)
return -1;
else if(index1==0)
{
if(comparecolor(p1[1].color,p2[1].color)>0)
return 1;
else if(comparecolor(p1[1].color,p2[1].color)<0)
return -1;
else if(comparecolor(p1[2].color,p2[2].color)>0)
return 1;
else if(comparecolor(p1[2].color,p2[2].color)<0)
return -1;
else if(comparecolor(p1[0].color,p2[0].color)>0)
return 1;
else if(comparecolor(p1[0].color,p2[0].color)<0)
return -1;
else
return 0;
}
else
{
if(comparecolor(p1[0].color,p2[0].color)>0)
return 1;
else if(comparecolor(p1[0].color,p2[0].color)<0)
return -1;
else if(comparecolor(p1[1].color,p2[1].color)>0)
return 1;
else if(comparecolor(p1[1].color,p2[1].color)<0)
return -1;
else if(comparecolor(p1[2].color,p2[2].color)>0)
return 1;
else if(comparecolor(p1[2].color,p2[2].color)<0)
return -1;
else
return 0;
}
}
else
return 1;
}
else if(isflush(p2)||isbomb(p2)||isline(p2)||isdouble(p2))
return -1;
else if(comparenum(p1[0].num,p2[0].num)>0)
return 1;
else if(comparenum(p1[0].num,p2[0].num)<0)
return -1;
else if(comparenum(p1[1].num,p2[1].num)>0)
return 1;
else if(comparenum(p1[1].num,p2[1].num)<0)
return -1;
else if(comparenum(p1[2].num,p2[2].num)>0)
return 1;
else if(comparenum(p1[2].num,p2[2].num)<0)
return -1;
else if(comparecolor(p1[0].color,p2[0].color)>0)
return 1;
else if(comparecolor(p1[0].color,p2[0].color)<0)
return -1;
else if(comparecolor(p1[1].color,p2[1].color)>0)
return 1;
else if(comparecolor(p1[1].color,p2[1].color)<0)
return -1;
else if(comparecolor(p1[2].color,p2[2].color)>0)
return 1;
else if(comparecolor(p1[2].color,p2[2].color)<0)
return -1;
return 0;
}
int testdata(Poker p[3])
{
int i,j;
for(i=0;i<3;i++)
{
if(!(p[i].num=='A'||p[i].num=='J'||p[i].num=='Q'||p[i].num=='K'||\
p[i].num>='2'&&p[i].num<='9'))
return -1;
if(!(p[i].color=='S'||p[i].color=='H'||p[i].color=='D'||p[i].color=='C'))
return -1;
for(j=i+1;j<3;j++)
{
if(p[i].color==p[j].color&&p[i].num==p[j].num)
return -1;
}
}
return 1;
}
int main()
{
Poker Apoker[3];
Poker Bpoker[3];
int i,flag;
char str[5];
printf("1. ");
for(i=0;i<3;i++)
{
scanf("%s",str);
Apoker[i].color=str[0];
Apoker[i].num=str[1];
}
printf("2. ");
for(i=0;i<3;i++)
{
scanf("%s",str);
Bpoker[i].color=str[0];
Bpoker[i].num=str[1];
}
sortpoker(Apoker);
sortpoker(Bpoker);
if(testdata(Apoker)==-1||testdata(Bpoker)==-1)
{
printf("Input Error!\n");
return 1;
}
flag=comparepoker(Apoker,Bpoker);
if(flag==1)
printf("1. Winner is A!\n");
else if(flag==-1)
printf("1. Winner is B!\n");
else
printf("1. Draw!\n");
printf("2. A: ");
for(i=0;i<3;i++)
{
printf("%c",Apoker[i].color);
if(Apoker[i].num=='1')
printf("10 ");
else
printf("%-3c",Apoker[i].num);
}
printf("\n");
printf("3. B: ");
for(i=0;i<3;i++)
{
printf("%c",Bpoker[i].color);
if(Bpoker[i].num=='1')
printf("10");
else
printf("%-3c",Bpoker[i].num);
}
printf("\n");
return 0;
}
这道题以前在知道看到过有人问,难度不大,但是要考虑的东西比较多,分太少不想做
我来回答,不过要给我一点时间。