您的位置:首页 > 其它

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

2018-03-14 11:06 561 查看
本文为转载,原博客地址: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
[cpp] view plain copy #include <iostream>  
#include <vector>  
#include <string>  
  
using namespace::std ;  
  
#define PRINT_FIRST           \  
   do {                       \  
      cout << first << endl ; \  
      return 0 ;              \  
   } while ( 0 )  
  
#define PRINT_NEXT            \  
   do {                       \  
      cout << next << endl ;  \  
      return 0 ;              \  
   } while ( 0 )  
  
#define PRINT( str )          \  
   do {                       \  
      cout << str << endl ;   \  
      return 0 ;              \  
   } while ( 0 )   
  
int count( string str ) {  
    string::size_type i = 0 ;  
    string::size_type k = 0 ;  
    int count = 0 ;  
    while ( ( k = str.find( ' ', i ) ) != string::npos ) {  
        ++ count ;  
        i = k + 1 ;  
    }  
    return ++ count ;  
}  
  
int main() {  
    string input ;  
    string first ;  
    string next ;  
    vector<string> MAX = { "joker JOKER", "JOKER joker" } ;  
    string POKER = "345678910JQKA2jokerJOKER" ;  
      
    getline( cin, input ) ;  
    first = input.substr( 0, input.find( '-' ) ) ;  
    next = input.substr( input.find( '-' ) + 1 ) ;  
    int countFirst = count( first ) ;  
    int countNext = count( next ) ;  
      
    if ( first == MAX[0] || first == MAX[1] ) PRINT( first ) ;   
    if ( next == MAX[0] || next == MAX[1] ) PRINT( next ) ;   
      
    if ( countFirst == countNext ) {  
        string f = first.substr( 0, first.find( ' ' ) ) ;  
        string n = next.substr( 0, next.find( ' ' ) ) ;  
        if ( POKER.find( f ) > POKER.find( n ) ) PRINT( first ) ;   
        else PRINT( next ) ;  
    }  
      
    if ( countFirst == 4 && countNext != 4 ) PRINT( first ) ;  
    else if ( countFirst != 4 && countNext == 4 ) PRINT( next ) ;  
    else cout << "ERROR" << endl ;  
      
    return 0 ;  
}  
#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;
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: