题解 | #扑克牌大小#
扑克牌大小
https://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb
#include <stdio.h> #include <string.h> /* 牌型 */ enum { sing = 1, doub, trip, bomb, conti, cp_king }; /* 判斷牌型 */ int typecheck (char str[5][5], int len) { if (len==1) return sing; else if (len==2) { if (str[0][1]=='o' || str[0][1]=='O') return cp_king; else return doub; } else if (len==3) return trip; else if (len==4) return bomb; else return conti; } /* 轉換為數字,方便比較大小 */ int switch2num(char ch, char ch_next) { if (ch>='3' && ch<='9') return ch-'3'; else if (ch=='1') return 7; else if (ch=='J') { if (ch_next!=0) return 14; else return 8; } else if (ch>='Q' && ch<='K') return 9+(ch-'Q'); else if (ch=='A') return 11; else if (ch=='2') return 12; else if (ch=='j') return 13; //小王 else return 14; //大王 } /* 牌型大小比較 */ int comp(char (*a)[5], char(*b)[5]) { if (switch2num(a[0][0], a[0][1])>switch2num(b[0][0], b[0][1])) return 1; else return -1; } /* 牌型打印 */ void Print(char (*str)[5], int column) { for (int i=0; i<column; i++) { if (i==column-1) { if (str[i][0]=='1') printf("%c%c\n",str[i][0],str[i][1]); //10特殊對待 else if (str[i][0]=='j' || (str[i][0]=='J'&&str[i][1]=='O')) printf("%c%c%c%c%c\n", str[i][0],str[i][1],str[i][2],str[i][3],str[i][4]); //大小王同理 else printf("%c\n",str[i][0]); } else { if (str[i][0]=='1') printf("%c%c ",str[i][0],str[i][1]); else if (str[i][0]=='j' || (str[i][0]=='J'&&str[i][1]=='O')) printf("%c%c%c%c%c ", str[i][0],str[i][1],str[i][2],str[i][3],str[i][4]); else printf("%c ",str[i][0]); } } } /* 入口函數 */ int main() { char input[50]; while (scanf("%[^\n]",input)!=EOF) { getchar(); //讀取掉\n字符 int length=strlen(input), i, abflag=0, iax=0, iay=0, ibx=0, iby=0; char a[5][5], b[5][5]; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); for (i=0; i<=length; i++) { if (input[i]=='-') { iax++; iay=0; abflag=1; continue; } else if (input[i]==' ') { if (abflag==0) { iax++; iay=0; } else { ibx++; iby=0; } } else if (input[i]=='\0') { ibx++; iby=0; } else { if (abflag==0) a[iax][iay++]=input[i]; else b[ibx][iby++]=input[i]; } } int typeA, typeB; // printf("iax=%d,ibx=%d\n",iax,ibx); typeA = typecheck(a, iax); typeB = typecheck(b, ibx); // printf("typeA=%d,typeB=%d\n", typeA,typeB); if (typeA==cp_king || typeB==cp_king) { printf("joker JOKER\n"); } else if (typeA==bomb || typeB==bomb) { if (typeA==bomb && typeB!=bomb) { Print(a, iax); } else if (typeA!=bomb && typeB==bomb) { Print(b, ibx); } else { if (comp(a, b)>0) Print(a, iax); else Print(b, ibx); } } else { if (typeA!=typeB) printf("ERROR\n"); else { if (comp(a, b)>0) Print(a, iax); else Print(b, ibx); } } } return 0; }