华为2016笔试-扑克牌大小比较游戏

本文为转载,原博客地址:http://blog.csdn.net/chengonghao/article/details/51815217
[编程题] 扑克牌大小
扑克牌游戏大家应该都比较熟悉了,一副牌由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)输入的两手牌不会出现相等的情况。

答案提示:
(1)除了炸弹和对王之外,其他必须同类型比较。
(2)输入已经保证合法性,不用检查输入是否是合法的牌。
(3)输入的顺子已经经过从小到大排序,因此不用再排序了.

输入描述:
输入两手牌,两手牌之间用“-”连接,每手牌的每张牌以空格分隔,“-”两边没有空格,如4 4 4 4-joker JOKER。


输出描述:
输出两手牌中较大的那手,不含连接符,扑克牌顺序不变,仍以空格隔开;如果不存在比较关系则输出ERROR。

输入例子:
4 4 4 4-joker JOKER

输出例子:
joker JOKER
 
  
  1. #include <iostream>  
  2. #include <vector>  
  3. #include <string>  
  4.   
  5. using namespace::std ;  
  6.   
  7. #define PRINT_FIRST           \  
  8.    do {                       \  
  9.       cout << first << endl ; \  
  10.       return 0 ;              \  
  11.    } while ( 0 )  
  12.   
  13. #define PRINT_NEXT            \  
  14.    do {                       \  
  15.       cout << next << endl ;  \  
  16.       return 0 ;              \  
  17.    } while ( 0 )  
  18.   
  19. #define PRINT( str )          \  
  20.    do {                       \  
  21.       cout << str << endl ;   \  
  22.       return 0 ;              \  
  23.    } while ( 0 )   
  24.   
  25. int count( string str ) {  
  26.     string::size_type i = 0 ;  
  27.     string::size_type k = 0 ;  
  28.     int count = 0 ;  
  29.     while ( ( k = str.find( ' ', i ) ) != string::npos ) {  
  30.         ++ count ;  
  31.         i = k + 1 ;  
  32.     }  
  33.     return ++ count ;  
  34. }  
  35.   
  36. int main() {  
  37.     string input ;  
  38.     string first ;  
  39.     string next ;  
  40.     vector<string> MAX = { "joker JOKER""JOKER joker" } ;  
  41.     string POKER = "345678910JQKA2jokerJOKER" ;  
  42.       
  43.     getline( cin, input ) ;  
  44.     first = input.substr( 0, input.find( '-' ) ) ;  
  45.     next = input.substr( input.find( '-' ) + 1 ) ;  
  46.     int countFirst = count( first ) ;  
  47.     int countNext = count( next ) ;  
  48.       
  49.     if ( first == MAX[0] || first == MAX[1] ) PRINT( first ) ;   
  50.     if ( next == MAX[0] || next == MAX[1] ) PRINT( next ) ;   
  51.       
  52.     if ( countFirst == countNext ) {  
  53.         string f = first.substr( 0, first.find( ' ' ) ) ;  
  54.         string n = next.substr( 0, next.find( ' ' ) ) ;  
  55.         if ( POKER.find( f ) > POKER.find( n ) ) PRINT( first ) ;   
  56.         else PRINT( next ) ;  
  57.     }  
  58.       
  59.     if ( countFirst == 4 && countNext != 4 ) PRINT( first ) ;  
  60.     else if ( countFirst != 4 && countNext == 4 ) PRINT( next ) ;  
  61.     else cout << "ERROR" << endl ;  
  62.       
  63.     return 0 ;  
  64. }  
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int count(string str)
{
	int length = str.length();
	int N = 1;
	for (int i = 0; i < length; i++){
		if (str[i] == ' '){
			N++;
		}
	}
	return N;
}

string getFirst(string str)
{
	int index = str.find(' ');               //find 一般是返回首次出现时的坐标 
	string first = str.substr(0, index);
	return first;
}

int main()
{
	string s, left, right;
	getline(cin, s);
	int countLeft, countRight;
	int midIndex, len, size;

	midIndex = s.find('-');
	left = s.substr(0, midIndex);             //substr用法
	right = s.substr( midIndex+1);

	countLeft = count(left);
	countRight = count(right);

	vector<string> doubleKing = {"joker JOKER","JOKER joker" };
	string poker = { "345678910JQKA2iokerJOKER" };

	if (left == doubleKing[0] || left == doubleKing[1]){
		cout << left << endl;
		return 0;                      //如果想不执行后面的语句,则直接输入return 0;
	}
	if (right == doubleKing[0] || right == doubleKing[1]){
		cout << right << endl;
		return 0;
	}
	if (countLeft == countRight){
		string firstLeft = getFirst(left);
		string firstRight = getFirst(right);
		int indexLeft = poker.find(firstLeft);
		int indexRight = poker.find(firstRight);
		if (indexLeft > indexRight){
			cout << left << endl;
		}
		else{
			cout << right << endl;
		}		
	}
	else if (countLeft == 4){
		cout << left << endl;
	}
	else if (countRight == 4){
		cout << right << endl;
	}
	else{
		cout << "ERROR" << endl;  //你他丫记得以后看输出要求,记得看要求,要求的输出格式
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值