首页 > 试题广场 >

扑克牌大小

[编程题]扑克牌大小
  • 热度指数:96430 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

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

数据范围:字符串长度:




输入描述:

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



输出描述:

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

示例1

输入

4 4 4 4-joker JOKER

输出

joker JOKER
只要输入规范就是简单难度,还是同类型才比较
a=input().split('-')
l=['3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2', 'joker', 'JOKER']
if 'joker JOKER' in a:
    print('joker JOKER')
else:
    b=[i.split() for i in a]
    if len(b[0])==len(b[1]):
        if l.index(b[0][0])>l.index(b[1][0]):
            print(a[0])
        else:
            print(a[1])
    elif len(b[0])==4:
        print(a[0])
    elif len(b[1])==4:
        print(a[1])
    else:
        print('ERROR')


发表于 2024-03-29 17:56:02 回复(0)
poker = {
    '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10,
    'J': 11, 'Q': 12, 'K': 13,
    'A': 14, '2': 15,
    'joker': 16, 'JOKER': 17
}

def compare(s1, s2: str):

    s1_l = list(s1.split(' '))
    s2_l = list(s2.split(' '))

    if 'joker JOKER' in s1&nbs***bsp;'joker JOKER' in s2:
        return 'joker JOKER'

    if len(s1_l) == len(s2_l):
        #即双方牌型相同
        if poker[s1_l[0]] >= poker[s2_l[0]]:
            return s1
        elif poker[s1_l[0]] < poker[s2_l[0]]:
            return s2
    #joker情况已比较,现在比较其中一个为炸弹的情况
    elif len(s1_l) == 4:
        return s1
    elif len(s2_l) == 4:
        return s2
    else:
        return 'ERROR'

a, b = input().split('-')
print(compare(a, b))

发表于 2024-03-28 22:13:21 回复(0)
dic = {'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':11,'Q':12,'K':13,'A':14,'2':15,'joker':16,'JOKER':17}
def isBoom(l): #定义炸弹不包含王炸
    if len(l) == 4 and len(set(l)) == 1:
        return True
    else:
        return False
while True:
    try:
        n1,n2 = input().split('-')#得到两手牌
        l1,l2 = sorted(n1.split()),sorted(n2.split())#将两手牌排序
        len1,len2 = len(l1),len(l2) #得到两手牌的张数 
        if len1 == len2 and (not isBoom(l1)&nbs***bsp;not isBoom(l2) ):#两手牌张数相等且不是炸弹
            if dic[l1[0]] > dic[l2[0]]:#比较第一张大小,谁大就输出谁
                print(n1)
            else:
                print(n2)
        else:#两手牌是炸弹 或者只有一手牌是炸弹
            if 'joker JOKER' in (n1,n2):#有王炸输出王炸
                print('joker JOKER')
            elif isBoom(l1) and not isBoom(l2):
                print(n1)
            elif not isBoom(l1) and isBoom(l2):
                print(n2)
            elif isBoom(l1) and isBoom(l2):#两个都是炸弹
                if dic[l1[0]] > dic[l2[0]]:#比较第一张大小,谁大就输出谁
                    print(n1)
                else:
                    print(n2)
            else:
                print('ERROR')
    except:
        break

发表于 2024-01-02 18:19:48 回复(0)
from re import S
danpai = {  
            "A":12,
            "2":13,
            "3":1,
            "4":2,
            "5":3,
            "6":4,
            "7":5,
            "8":6,
            "9":7,
            "10":8,
            "J":9,
            "Q":10,
            "K":11,
            "joker":14,
            "JOKER":15
            }

duizi = {
            "1 1":12,
            "2 2":13,
            "3 3":1,
            "4 4":2,
            "5 5":3,
            "6 6":4,
            "7 7":5,
            "8 8":6,
            "9 9":7,
            "10 10":8,
            "J J":9,
            "Q Q":10,
            "K K":11,

}

sanzhang = {
            "1 1 1":12,
            "2 2 2":13,
            "3 3 3":1,
            "4 4 4":2,
            "5 5 5":3,
            "6 6 6":4,
            "7 7 7":5,
            "8 8 8":6,
            "9 9 9":7,
            "10 10 10":8,
            "J J J":9,
            "Q Q Q":10,
            "K K K":11,

}

shunzi = {
            "3 4 5 6 7":1,
            "4 5 6 7 8":2,
            "5 6 7 8 9":3,
            "6 7 8 9 10":4,
            "7 8 9 10 J":5,
            "8 9 10 J Q":6,
            "9 10 J Q K":7,
            "10 J Q K A":8
            }

zhandan = {
            "3 3 3 3":1,
            "4 4 4 4":2,
            "5 5 5 5":3,
            "6 6 6 6":4,
            "7 7 7 7":5,
            "8 8 8 8":6,
            "9 9 9 9":7,
            "10 10 10 10":8,
            "J J J J":9,
            "Q Q Q Q":10,
            "K K K K":11,
            "1 1 1 1":12,
            "2 2 2 2":13,
            "joker JOKER":14,  
            "JOKER joker":14  
}

array = []
array.append(danpai)
array.append(duizi)
array.append(sanzhang)
array.append(shunzi)
array.append(zhandan)
x = input().split("-")
for i in range(5):
    if x[0] in array[i]:
        mm = i
    if x[1] in array[i]:
        nn = i

if mm == nn:
    if array[mm][x[0]] < array[mm][x[1]]:
        print(x[1])
    elif array[mm][x[0]] > array[mm][x[1]]:
        print(x[0])
    elif array[mm][x[0]] == array[mm][x[1]]:
        print(x[0])
elif mm == 4 and nn !=4:
    print(x[0])
elif nn == 4 and mm != 4:
    print(x[1])
elif nn != 4 and mm != 4 and nn != mm:
    print("ERROR")
发表于 2023-06-11 10:42:26 回复(0)
# 定义牌的类型
# 0 = 链子, 1 = 单牌, 2 = 对牌 ,3 = 蛋子, 4=炸弹, 5 = 王炸, 6 = 未知
def check_type(pai_li: list):
    if len(pai_li) == 1:
        return 1
    elif len(pai_li) == 2:
        if ["JOKER", "joker"] == sorted(pai_li):
            return 5
        return 2
    elif len(pai_li) == 3 and len(set(pai_li)) == 1:
        return 3
    elif len(pai_li) == 4 and len(set(pai_li)) == 1:
        return 4
    elif len(pai_li) >= 5 and len(set(pai_li)) >= 5:
        return 0
    else:
        return 6
while True:
    try:
        t1, t2 = input().split("-")
        t1_li = t1.split()
        t2_li = t2.split()
        # 定义从小到大规则列表
        guize_li = [
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "10",
            "J",
            "Q",
            "K",
            "A",
            "2",
            "joker",
            "JOKER",
        ]
        # 定义2手牌的类型,方便比较大小
        t1_type = check_type(t1_li)
        t2_type = check_type(t2_li)
        # 如果输入的牌 属于复杂的牌,没法对比
        if t1_type > 5 or t2_type > 5:
            print("ERROR")
        # 如果有王炸,王炸最大
        elif t1_type == 5 or t2_type == 5:
            print("joker JOKER")
        # 如果其中一个手里 炸弹
        elif t1_type == 4 or t2_type == 4:
            # 先看2个都是不是炸弹,都是炸弹比大小
            if t1_type == 4 and t2_type == 4:
                print(t1 if guize_li.index(t1_li[0]) > guize_li.index(t2_li[0]) else t2)
            # 否则炸弹大
            else:
                print(t1 if t1_type == 4 else t2)
        # 对比链子
        elif t1_type == 0 and t2_type == 0 and len(t1_li) == len(t2_li):
            t1_num = sum([guize_li.index(i) for i in t1_li])
            t2_num = sum([guize_li.index(i) for i in t2_li])
            print(t1 if t1_num > t2_num else t2)
        # 剩余的就是 单排-单排,对子-对子, 蛋子-蛋子
        else:
            # 相同的类型比索引
            if t1_type == t2_type:
                print(t1 if guize_li.index(t1_li[0]) > guize_li.index(t2_li[0]) else t2)
            # 不同的类型没法比较输出ERROR
            else:
                print("ERROR")
    except:
        break
发表于 2023-05-17 23:36:31 回复(0)
cards=input().split('-')
card1,card2=cards[0].split(' '),cards[1].split(' ')
while True:
    if card1==['joker','JOKER']&nbs***bsp;card2==['joker','JOKER']:
        print('joker JOKER')
        break
    if len(card1)==4 and len(card2)!=4:
        print(' '.join(card1))
        break
    if len(card2)==4 and len(card1)!=4:
        print(' '.join(card2))
        break
    if len(card1)!=len(card2):
        print('ERROR')
        break
    order='3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER'
    print(' '.join(card1) if order.find(card1[0])>order.find(card2[0]) else ' '.join(card2))
    break

发表于 2023-05-05 11:28:32 回复(0)
c1, c2 = input().split('-')
s1, s2 = list(c1.split()), list(c2.split())
l1, l2 = len(s1), len(s2)
dic = {'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':11,'Q':12,'K':13,'A':14,'2':15,'joker':16,'JOKER':17}
# joker与对子区分开,直接输出即可
if c1 == 'joker JOKER'&nbs***bsp;c2 == 'joker JOKER':
    print('joker JOKER')
else:
    # 先判别错误
    if l1 in [1, 2, 3, 5] and l2 in [1, 2, 3, 5] and l1 != l2:
        print('ERROR')
    else:
        # 再处理炸弹,先判断炸弹+普通牌,否则直接按牌面判断即可
        if l1 == 4 and l2 != 4:
            print(c1)
        elif l2 == 4 and l1 != 4:
            print(c2)
        else:
            # 同类型比较第一个数字大小就行
            if dic[s1[0]] < dic[s2[0]]:
                print(c2)
            else:
                print(c1)


发表于 2023-04-14 00:15:39 回复(0)
咱就是说,这种题目如果显示错误用例就so ez,但把错误用例都遮住的话怎么也写不对。。。
a,b=input().split('-')
a=a.split()
b=b.split()
dic={'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':11,'Q':12,'K':13,'A':14,'2':15,'joker':16,'JOKER':17}
if set(a)=={'joker','JOKER'}&nbs***bsp;set(b)=={'joker','JOKER'} :
    print('joker JOKER')
elif ((len(set(a))==1 and len(a)==4)&nbs***bsp;(len(set(b))==1 and len(b)==4)) and len(a)!=len(b):
    if len(a)==4:
        print(' '.join(a))
    else:
        print(' '.join(b))
elif len(a)==len(b):
    if len(a)==2 and len(set(a))==len(set(b))==1:
        if dic[a[0]]>dic[b[0]]:
            print(' '.join(a))
        else:
            print(' '.join(b))
    elif len(a)==3 and len(set(a))==len(set(b))==1:
        if dic[a[0]]>dic[b[0]]:
            print(' '.join(a))
        else:
            print(' '.join(b))
    elif len(a)==1:
        if dic[a[0]]>dic[b[0]]:
            print(' '.join(a))
        else:
            print(' '.join(b))
    elif len(a)==4 and len(set(a))==len(set(b))==1:
        if dic[a[0]]>dic[b[0]]:
            print(' '.join(a))
        else:
            print(' '.join(b))
    elif len(a)==5:
        if dic[a[0]]>dic[b[0]]:
            print(' '.join(a))
        elif dic[a[0]]<dic[b[0]]:
            print(' '.join(b))
else:
    print('ERROR')

发表于 2023-03-17 05:23:19 回复(0)
s1, s2 = input().split('-')

d  = {
    '3':3,
    '4':4,
    '5':5,
    '6':6,
    '7':7,
    '8':8,
    '9':9,
    '10':10,
    'J':11,
    'Q':12,
    'K':13,
    'A':14,
    '2':15,
    'joker': 16,
    'JOKER': 17
}
if s1 == 'joker JOKER'&nbs***bsp;s2 == 'joker JOKER':
    print('joker JOKER')

elif len(s1.split()) != len(s2.split()):
    if len(s1.split()) == 4 and len(set(s1.split())) == 1:
        print(s1)
    elif len(s2.split()) == 4 and len(set(s2.split())) == 1:
        print(s2)

    else:
        print("ERROR")
    
elif len(s1.split()) == len(s2.split()):
    # print(s1.split())
    # print(s2.split())

    if d[s1.split()[0]] > d[s2.split()[0]]:
        print(s1)
    else:
        print(s2)

发表于 2023-03-05 22:37:10 回复(0)
pok = input().split("-")
po1 = list(pok[0].split(" "))
po2 = list(pok[1].split(" "))
dic = {
    "3": 1,
    "4": 2,
    "5": 3,
    "6": 4,
    "7": 5,
    "8": 6,
    "9": 7,
    "10": 8,
    "J": 9,
    "Q": 10,
    "K": 11,
    "A": 12,
    "2": 13,
    "joker": 14,
    "JOKER": 15,
}
list1 = []
list2 = []
for i in po1:
    list1.append(dic[i])
list1.sort()
for i in po2:
    list2.append(dic[i])
list2.sort()
if po1 == ["joker", "JOKER"]&nbs***bsp;po1 == ["JOKER", "joker"]:
    print(po1[0], po1[1])
elif po2 == ["joker", "JOKER"]&nbs***bsp;po2 == ["JOKER", "joker"]:
    print(po2[0], po2[1])
elif len(po1) == len(po2) and len(po1) in [1, 2, 3]:
    if list1[0] > list2[0]:
        print(" ".join(po1))
    else:
        print(" ".join(po2))
elif len(po1) == 4 and len(po2) != 4:
    print(" ".join(po1))
elif len(po2) == 4 and len(po1) != 4:
    print(" ".join(po2))
elif len(po2) == 4 and len(po1) == 4:
    if list1[0] > list2[0]:
        print(" ".join(po1))
    else:
        print(" ".join(po2))
elif len(po1) == 5 and len(po2) == 5:
    if list1[0] > list2[0]:
        print(" ".join(po1))
    else:
        print(" ".join(po2))
elif len(po1) != len(po2):
    print("ERROR")

发表于 2022-12-15 15:45:23 回复(0)
老实巴交的办法,一种一种比……
import sys

cards = input().split('-')
card_a = cards[0]
card_b = cards[1]
# if '10' in card_a:
#     card_a = card_a.replace('10','0')
# if '10' in card_b:
#     card_b = card_b.replace('10','0')

card_a_c = card_a.split(' ')
card_b_c = card_b.split(' ')
dicts = {'2':15,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':11,'Q':12,'K':13,'A':14,'joker':15,'JOKER':16}

def cards_compare(card_a,card_b):
    #王炸最大
    if card_a == 'joker JOKER':
        return card_a
    elif card_b == 'joker JOKER':
        return card_b

    #炸弹和其他牌比较,炸弹最大
    if len(card_a_c) == 4 and len(card_b_c) != 4:
        return card_a
    elif len(card_a_c) != 4 and len(card_b_c) == 4:
        return card_b
    elif len(card_a_c) == 4 and len(card_b_c) == 4:
        if dicts[card_a_c[0]] > dicts[card_b_c[0]]:
            return card_a
        else:
            return card_b

    #三个比较
    if len(card_a_c) == 3 and len(card_b_c) == 3:
        if dicts[card_a_c[0]] > dicts[card_b_c[0]]:
            return card_a
        else:
            return card_b

    #对子比较
    if len(card_a_c) == 2 and len(card_b_c) == 2:
        if dicts[card_a_c[0]] > dicts[card_b_c[0]]:
            return card_a
        else:
            return card_b

    #单个比较
    if (card_a == 'joker'&nbs***bsp;card_a == 'JOKER') and dicts[card_b] < dicts[card_a]:
        return card_a
    elif (card_b == 'joker'&nbs***bsp;card_b == 'JOKER') and dicts[card_b] > dicts[card_a]:
        return card_b
    elif len(card_a_c) == 1 and len(card_b_c) == 1:
        if dicts[card_a] > dicts[card_b]:
            return card_a
        else:
            return card_b

    #顺子比较
    if len(card_a_c) == 5 and len(card_b_c) == 5:
        if dicts[card_a_c[-1]] > dicts[card_b_c[-1]]:
            return card_a
        else:
            return card_b

    return 'ERROR'

print(cards_compare(card_a,card_b))


发表于 2022-11-23 22:35:27 回复(0)
写完以后看了排行榜才发现 原来两手牌的长度和牌型是相同的..... 就我一个人傻乎乎写成遍历不定长度手牌后寻找最大牌型了
from copy import deepcopy
s = input()
hardcopy = deepcopy(s)

s = s.replace('joker','16')
s = s.replace('JOKER','17')
s = s.replace('J','11')
s = s.replace('Q','12')
s = s.replace('K','13')
s = s.replace('A','14')
s = s.replace('2','15')

s1,s2 = s.split('-')
s1 = s1.split()
s2 = s2.split()

if '16' in s1 and '17' in s1:
    print('joker JOKER')
    exit(0)

if '16' in s2 and '17' in s2:
    print('joker JOKER')
    exit(0)


def checker(ls):
    counter = 0 
    save = ls[0]
    tmp = list()
    res = list()
    for i in range(len(ls)):
        val = ls[i]
        record = ls.count(ls[i])
        if record > counter:
            save = val 
            counter = record
        elif record == counter and val > save:
            save = val 
        if tmp:
            if int(ls[i]) == int(tmp[-1])+1:
                tmp.append(int(ls[i]))
            else:
                if len(tmp) >= 5:
                    tmp.sort()
                    res.append([tmp[-5],5])
                tmp = list()
        if not tmp:
            tmp.append(int(ls[i]))
        if i == len(ls)-1 and len(tmp) >= 5:
            res.append([tmp[-5],5])

    if counter == 4:
        res.append([save,20])  
    else:
        res.append([save,counter])  
    res = sorted(res,key=lambda x:x[1],reverse=True)
    res = res[-1]
    return res 

res1 = checker(s1)
res2 = checker(s2)

f1,f2 = hardcopy .split('-')
if res1[1] == res2[1]:
    if int(res1[0]) > int(res2[0]):
        print(f1)
    else:
        print(f2)
else:
    if res1[1] == 20&nbs***bsp;res2[1] == 20:
        if res1[1] == 20:
            print(f1)
        else:
            print(f2)
    else:
        print('ERROR')


发表于 2022-11-19 17:53:26 回复(0)
自我安排一波
while True:
    try:
        st=input()
        st1,st2=st.split('-')
        com=['3','4','5','6','7','8','9','10','J','Q','K','A','2','joker','JOKER']
        lst1,lst2=st1.split(),st2.split()
        m,n=len(lst1),len(lst2)
        if lst1==['joker','JOKER']:
            print(st1)
        elif lst2==['joker','JOKER']:
            print(st2)
        else:
            if m!=n and n==4:
                print(st2)
            elif m!=n and m==4:
                print(st1)
            elif m==n:
                if com.index(lst1[0])<com.index(lst2[0]):
                    print(st2)
                elif com.index(lst1[0])==com.index(lst2[0]):
                    print('ERROR')
                else:
                    print(st1)
            else:
                print('ERROR')
    except:
        break
发表于 2022-09-20 00:24:21 回复(0)
# 读懂题意真难! 反复读
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
card = ['3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2', 'joker', 'JOKER']
s1, s2 = input().split('-')
nums1, nums2 = s1.split(), s2.split()
if len(nums1) == len(nums2):  # 两人手牌相同且可以比较大小,则比较最小一位.(同为顺子, 四个,三个, 对子,  单张)
    if card.index(nums1[0]) > card.index(nums2[0]):
        print(s1)
    else:
        print(s2)
else:
    if 'joker JOKER' in [s1, s2]:  # 对王可以与其他牌比较, 且对王最大
        print('joker JOKER')
    elif nums1.count(nums1[0]) == 4:  # 4 4 4 4-2 2  四个可以与其他牌比较, 四个最大
        print(s1)
    elif nums2.count(nums2[0]) == 4:
        print(s2)
    else:
        print('ERROR')

发表于 2022-04-09 12:53:30 回复(0)
# 2022/3/12 16:03 - 时间
# PyCharm - 当前集成开发环境
# 思路 : 类型一样和类型不一样的
power = ["3","4","5","6","7","8","9","10","J","Q","K","A","2","joker","JOKER"]
while True:
    try:
        a,b = input().split('-')
        sas = a.split()
        sbs = b.split()
        # 类型一样
        if len(sas) == len(sbs):
            if power.index(sas[0])>power.index(sbs[0]):
                print(a)
            else:
                print(b)
        #类型不一样
        else:
            # 有王炸直接输出王炸
            if a =="joker JOKER"&nbs***bsp;b =="joker JOKER":
                print("joker JOKER")
            # a有炸弹
            elif len(sas)==4:
                print(a)
            # b有炸弹
            elif len(sbs)==4:
                print(b)
            #其他类型,无法比较
            else:
                print("ERROR")
    except:
        break

发表于 2022-03-12 16:17:19 回复(0)
cards = ["3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "joker", "JOKER"]
d = {}
for i, c in enumerate(cards):
    d[c] = i

while True:
    try:
        c1, c2 = input().split('-')
        l1, l2 = c1.split(), c2.split()
        res = ""
        if 'joker JOKER' in (c1, c2):
            res = 'joker JOKER'
        elif len(l1) == len(l2):
            res = c1 if d[l1[0]] > d[l2[0]] else c2
        else:
            if len(l1) == 4 and len(set(l1)) == 1:
                res = c1
            elif len(l2) == 4 and len(set(l2)) == 1:
                res = c2
            else:
                res = 'ERROR'
        print(res)
    except:
        break

发表于 2021-12-01 00:13:15 回复(0)