牛客[编程题] HJ88 扑克牌大小

发布时间 2023-11-09 17:47:35作者: 张德长

 

HJ88 扑克牌大小
较难  通过率:28.08%  时间限制:1秒  空间限制:32M
 

描述

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

数据范围:字符串长度:3\le s\le 10\

 

 

输入描述:

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

输出描述:

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

示例1

输入:
4 4 4 4-joker JOKER
输出:
joker JOKER

 

using System;
using System.Collections.Generic;
public class Program {
    public static void Main() {
        string line;
        while ((line = System.Console.ReadLine()) != null) {
            // 注意 while 处理多个 case
            var arr = line.Split('-');
            string s1 = arr[0];
            string s2 = arr[1];
            var arr1 = s1.Split(' ');
            var arr2 = s2.Split(' ');
            var dic = GetDic();
            string res = string.Empty ;
            if (arr1.Length == 1 && arr2.Length == 1) {
                if (dic[arr1[0]] > dic[arr2[0]]) {
                    res = s1;
                } else {
                    res = s2;
                }
            } else if (arr1.Length == 3 && arr2.Length == 3) {
                if (dic[arr1[0]] > dic[arr2[0]]) {
                    res = s1;
                } else {
                    res = s2;
                }
            } else if (arr1.Length == 5 && arr2.Length == 5) {
                if (dic[arr1[0]] > dic[arr2[0]]) {
                    res = s1;
                } else {
                    res = s2;
                }
            } else if (arr1.Length == 2 && arr2.Length == 2) {
                if (IsDouble(arr1) && IsDouble(arr2)) {
                    if (dic[arr1[0]] > dic[arr2[0]]) {
                        res = s1;
                    } else {
                        res = s2;
                    }
                } else if (IsDouble(arr1) && !IsDouble(arr2)) {
                    res = s2;
                } else if (!IsDouble(arr1) && IsDouble(arr2)) {
                    res = s1;
                }
            } else if (arr1.Length == 2 && arr2.Length == 4) {
                if (!IsDouble(arr1)) {
                    res = s1;
                } else {
                    res = s2;
                }
            } else if (arr1.Length == 4 && arr2.Length == 2) {
                if (!IsDouble(arr2)) {
                    res = s2;
                } else {
                    res = s1;
                }
            } else if (arr1.Length == 2) {
                if (!IsDouble(arr1)) {
                    res = s1;
                } else {
                    res = "ERROR";
                }
            } else if (arr2.Length == 2) {
                if (!IsDouble(arr2)) {
                    res = s2;
                } else {
                    res = "ERROR";
                }
            } else if (arr1.Length == 4 && arr2.Length == 4) {
                if (dic[arr1[0]] > dic[arr2[0]]) {
                    res = s1;
                } else {
                    res = s2;
                }
            } else if (arr1.Length == 4) {
                res = s1;

            } else if (arr2.Length == 4) {
                res = s2;
            } else {
                res = "ERROR";
            }
            Console.WriteLine(res);
            continue;
        }
    }
    static bool IsDouble(string[] arr) {
        if (arr[0] == arr[1]) {
            return true;
        }
        return false;
    }

    public static Dictionary<string, int> GetDic() {
        Dictionary<string, int> dic = new Dictionary<string, int>();
        dic.Add("3", 3);
        dic.Add("4", 4);
        dic.Add("5", 5);
        dic.Add("6", 6);
        dic.Add("7", 7);
        dic.Add("8", 8);
        dic.Add("9", 9);
        dic.Add("10", 10);
        dic.Add("J", 11);
        dic.Add("Q", 12);
        dic.Add("K", 13);
        dic.Add("A", 14);
        dic.Add("2", 15);
        dic.Add("joker", 16);
        dic.Add("JOKER", 17);
        return dic;
    }

}