0
点赞
收藏
分享

微信扫一扫

A、B两个人使用一副扑克牌打斗地主,其中,3--10---J--A之间5张及以上的连续牌称为顺子,输入A手中的牌,在输入A已经出的牌,打印出B可以出的最大顺子。如果B的牌不存在顺子,打印NO-CHA

十里一走马 2022-04-27 阅读 45
python
例如:
输入 A手中牌为:3-3-3-3-4-4-5-5-6-7-8-9-10-J-Q-K-A
再输入A已经出的牌:4-5-6-7-8-8-8
得到B可以出的最大顺子为:9-10-J-Q-K-A
例如:
输入 A手中牌为:3-3-3-3-8-8-8-8
再输入A已经出的牌:K-K-K-K
得到B可以出的最大顺子为:NO-CHAIN
def char_to_int(str_list:list):
    for i in range(len(str_list)):
        if str_list[i] == 'J':
            str_list[i] = '11'
        elif str_list[i] == 'Q':
            str_list[i] = '12'
        elif str_list[i] == 'K':
            str_list[i] = '13'
        elif str_list[i] == 'A':
            str_list[i] = '14'
    return str_list

def int_to_char(str_list:list):
    for i in range(len(str_list)):
        if str_list[i] == '11':
            str_list[i] = 'J'
        elif str_list[i] == '12':
            str_list[i] = 'Q'
        elif str_list[i] == '13':
            str_list[i] = 'K'
        elif str_list[i] == '14':
            str_list[i] = 'A'
    return str_list

A_shou = ['3', '3', '3', '3', '4', '4', '5', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
A_chu = ['4', '5', '6', '7', '8', '8', '8']
A_all = A_shou + A_chu
A_all = char_to_int(A_all)    # 把JQKA转为数字
A_all = list(map(int,A_all))    #把A手里的牌转为数字
all_pai = list(range(3,15))*4   #所有扑克牌
for n in A_all:       #所有的牌减掉A的所有牌,得到B的所有牌
    all_pai.remove(n)
B_all = list(set(all_pai))    #得到去重后B所有的牌
B_all.sort(reverse=True)

all_list = []
sub_list = []
for x in B_all:
    sub_list.append(x)
    if x-1 not in B_all:
        if len(sub_list)!=1:
            all_list.append(sub_list)
        sub_list = []
print(all_list)
all_list.sort(key=len,reverse=True)
if len(all_list[0])<5:
    print('NO-CHAIN')
else:
    shunzi = list(map(str,all_list[0]))
    shunzi = int_to_char(shunzi)
    print("-".join(shunzi[::-1]))
举报
0 条评论