扑克牌游戏大家应该都比较熟悉了,一副牌由54张组成,含3~A、2各4张,小王1张,大王1张。牌面从小到大用如下字符和字符串表示(其中,小写joker表示小王,大写JOKER表示大王):
3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER
输入两手牌,两手牌之间用"-"连接,每手牌的每张牌以空格分隔,"-"两边没有空格,如:4 4 4 4-joker JOKER。
请比较两手牌大小,输出较大的牌,如果不存在比较关系则输出ERROR。
基本规则:
(1)输入每手牌可能是个子、对子、顺子(连续5张)、三个、炸弹(四个)和对王中的一种,不存在其他情况,由输入保证两手牌都是合法的,顺子已经从小到大排列;
(2)除了炸弹和对王可以和所有牌比较之外,其他类型的牌只能跟相同类型的存在比较关系(如,对子跟对子比较,三个跟三个比较),不考虑拆牌情况(如:将对子拆分成个子);
(3)大小规则跟大家平时了解的常见规则相同,个子、对子、三个比较牌面大小;顺子比较最小牌大小;炸弹大于前面所有的牌,炸弹之间比较牌面大小;对王是最大的牌;
(4)输入的两手牌不会出现相等的情况。
#include "stdio.h" #include "stdlib.h" #include "string.h" #define MAX_PATH 256 int getValue(char *buffer) { if (strcmp(buffer,"3")==0) { return 3; } else if (strcmp(buffer,"4")==0) { return 4; } else if (strcmp(buffer,"5")==0) { return 5; } else if (strcmp(buffer,"6")==0) { return 6; } else if (strcmp(buffer,"7")==0) { return 7; } else if (strcmp(buffer,"8")==0) { return 8; } else if (strcmp(buffer,"9")==0) { return 9; } else if (strcmp(buffer,"10")==0) { return 10; } else if (strcmp(buffer,"10")==0) { return 10; } else if (strcmp(buffer,"J")==0) { return 11; } else if (strcmp(buffer,"Q")==0) { return 12; } else if (strcmp(buffer,"K")==0) { return 13; } else if (strcmp(buffer,"A")==0) { return 14; } else if (strcmp(buffer,"2")==0) { return 15; } else if (strcmp(buffer,"joker")==0) { return 16; } else if (strcmp(buffer,"JOKER")==0) { return 17; } } int impl(char *buffer) { char *atmp = strtok(buffer,"-"); char *btmp = strtok(NULL,"-"); char a[MAX_PATH] = {0}; char b[MAX_PATH] = {0}; memcpy(a,atmp,strlen(atmp)); memcpy(b,btmp,strlen(btmp)); if(strstr(a,"joker")) { printf(atmp); return 0; } else if(strstr(b,"joker")) { printf(btmp); return 0; } int numa = 1; for (int i=0;i<strlen(a);i++) { if (a[i]==' ') { numa++; } } int numb = 1; for (int i=0;i<strlen(b);i++) { if (a[i]==' ') { numb++; } } if (numa==4 && numb!=4) { printf(atmp); return 0; } if (numa!=4 && numb==4) { printf(btmp); return 0; } if (numa==4 && numb==4) { int at = getValue(strtok(a," ")); int bt = getValue(strtok(b," ")); if (at>bt) { printf(atmp); return 0; } else { printf(btmp); return 0; } } if (numa!=4 && numb!=4) { if (numa==1) { int at = getValue(a); int bt = getValue(b); if (at>bt) { printf(atmp); return 0; } else { printf(btmp); return 0; } } else { int at = getValue(strtok(a," ")); int bt = getValue(strtok(b," ")); if (at>bt) { printf(atmp); return 0; } else { printf(btmp); return 0; } } } } int main() { char buffer[MAX_PATH] = {0}; gets(buffer); impl(buffer); // char test[20][20] = {"6-A","6-joker","3 3-10 10","3 3 3-2 2 2","J J J J-Q Q Q Q","J J J J-2 2 2","J J J J-joker JOKER"}; // for (int i=0;i<7;i++) // { // impl(test[i]); // } return 0; }
原文地址:http://blog.csdn.net/hepfei90/article/details/44055643