牛客[编程题] HJ88 扑克牌大小
描述
扑克牌游戏大家应该都比较熟悉了,一副牌由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; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理