蓝桥杯2024省赛PythonB组——拉马车

题目链接

https://www.lanqiao.cn/problems/101/learning/?page=1&first_category_id=1&name=%E6%8B%89%E9%A9%AC%E8%BD%A6

题目内容

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

解题思路

按照出牌规则模拟写代码即可。容易漏的是,当A空的时候,并且桌子上没有可以获得的牌子的时候就已经可以跳出来了,B不用再出牌,没跳出来的话,B就会少了一张牌子,导致没通过。

import os
import sys

# 请在此输入您的代码
A = list(input())
B = list(input())
# print(A,B)
flag_A = 1 # A先出牌
flag_B = 0
chupai = list()
while A != [] and B != []:
    if flag_A:
        flag_A = 0
        flag_B = 1
        A_chupai = A.pop(0)
        if A_chupai in chupai:
            # 找到出的牌子在序列中的位置
            position = chupai.index(A_chupai)
            chupai.append(A_chupai)
            huo_paizi = chupai[position:]
            A.extend(huo_paizi[::-1])
            # 更新牌
            chupai = chupai[:position]
            flag_A = 1
            flag_B = 0
        else:
            chupai.append(A_chupai)
        if A == []:
            break
    if flag_B:
        flag_B = 0
        flag_A = 1
        B_chupai = B.pop(0)
        if B_chupai in chupai:
            # 找到出的牌子在序列中的位置
            position = chupai.index(B_chupai)
            chupai.append(B_chupai)
            huo_paizi = chupai[position:]
            B.extend(huo_paizi[::-1])
            # 更新牌
            chupai = chupai[:position]
            flag_B = 1
            flag_A = 0
        else:
            chupai.append(B_chupai)
    if B == []:
        break

if A:
    print(''.join(A))
elif B:
    print(''.join(B))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值