码迷,mamicode.com
首页 > 其他好文 > 详细

HuaweiOJ-扑克牌大小

时间:2014-07-06 00:53:23      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:huaweioj   c   

【问题描述】

扑克牌游戏大家应该都比较熟悉了,一副牌由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)输入的两手牌不会出现相等的情况。

 
   

花了两个小时写完,在自己电脑上测试没出现什么问题。提交后总是出现Error 10, Error 129这两个错误,改了好久都没有成功。希望大家帮忙讨论下这个错误该怎么解决。

【代码】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
type表示输入的牌的类型。
    0	单个王
	1	个子(不包含单个王)
	2	对子
	3	顺子
	4	三个
	5	炸弹
	6	对王
*/

void getStrType(char *poker, char *firstPoker, int  *type)
{
	int i, len;
	int count;
	len = strlen(poker);
	i = 0;
	while (poker[i] != ' ' && i <= len)
	{
		firstPoker[i] = poker[i]; 
		i++;
	}
	firstPoker[i] = '\0';
	i = count = 0;
	while(i < len)
	{
		if(poker[i] == ' ')
			count++;
		i++;
	}

	if (0 == count)
	{
		if(strlen(firstPoker) < 5)			
			*type = 1;
		else
			*type = 0; 
	}
	else if (1 == count)
	{
		if (strlen(firstPoker) < 5)
			*type = 2;
		else 
			*type = 6;
	}
	else if (2 == count)
		*type = 4;
	else if (3 == count)
		*type = 5;
	else
		*type = 3; 

}


int pokerCmp(char *str)
{
	int i, j, len;
	int cmp;
	char poker1[128], poker2[128];
	char firstPoker1[32], firstPoker2[32];
	int type1, type2;
	len = strlen(str);
	while(str[len - 1] == ' ')
		len--;
	if(0 == len)
		return 0;
	if(128 < len)
		return -1;
	i = j = 0;
	while (str[i] != '-')
		poker1[j++] = str[i++];
	poker1[j] = '\0';
	i++; j = 0; 
	while( i <= len)
		poker2[j++] = str[i++];

	if( 0 == strlen(poker1) || 0 == strlen(poker2))
		return 0;

	getStrType(poker1, firstPoker1, &type1);
	getStrType(poker2, firstPoker2, &type2);

	/*比较大小*/
	if((6 == type1) || (5 == type1 && 5 > type2))
		printf("%s", poker1);
	else if((6 == type2) || (5 == type2 && 5 > type1))
		printf("%s", poker2);
	else if( 0 == type1 && 1 == type2)
		printf("%s", poker1);
	else if(0 == type2 && 1 == type1)
		printf("%s", poker2);
	else if(type1 == type2)
	{
		if(type1 == 0)
		{
			if('J' == poker1[0])
				printf("%s", poker1);
			else
				printf("%s", poker2);
		}
		else if( type1 == 3)	//顺子, A,2,3,4,5和2,3,4,5,6不能构成顺子
		{
			cmp = strcmp(firstPoker1, firstPoker2);
			if(0 > cmp)
				printf("%s", poker2);
			else
				printf("%s", poker1);
		}
		else //1, 2, 4, 5
		{
			if('2' == firstPoker1[0] || ('A' == firstPoker1[0] && '2' != firstPoker2[0]))
				printf("%s", poker1);
			else if('2' == firstPoker2[0] || ('A' == firstPoker2[0] && '2' != firstPoker1[0]))
				printf("%s", poker2);
			else
			{
				cmp = strcmp(firstPoker1, firstPoker2);
				if(0 > cmp)
					printf("%s", poker2);
				else
					printf("%s", poker1);
			}
		}
	}
	else
		printf("ERROR");

	return 1;
}

int main(void)
{
	char str[128];
	gets(str);
	pokerCmp(str);
	return 0;
}


HuaweiOJ-扑克牌大小,布布扣,bubuko.com

HuaweiOJ-扑克牌大小

标签:huaweioj   c   

原文地址:http://blog.csdn.net/jjjcainiao/article/details/36870543

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!