|
def find_max(nStr):#每轮首次出牌
maxCnt, maxInd = 0, 0
for i in range(len(nStr)):
if eval(nStr) > maxCnt:
maxCnt = eval(nStr)
maxInd = i
return maxCnt, maxInd
def find_deal(nStr, nowLen, cardId):#接牌
for i in range(len(nStr)):
if eval(nStr) == nowLen:
if i > cardId:
return i
return -1
pList = []#出完牌的列表
turn = 0#当前出牌玩家
nowLen = 0#当前出牌张数(0代表本轮首次出牌)
bidder = 0#上一个出牌的玩家
cardId = 0#上一个出牌的点数
times = 0#仔细读了之后感觉是个flag
while len(pList) < (n - 1):#全部出完牌后退出循环
if turn == bidder:#上一次出牌后无人接牌
times += 1#标记一下
if (turn + 1) in pList:#当前玩家已经出完了
turn = (turn + 1) % n#顺延下一个玩家
continue
print(pCount[turn])
print(cards[cardId], nowLen)
if nowLen == 0:#如果是本轮首次出牌
times = 0
nowLen, cardId = find_max(pCount[turn])#确定出牌的张数和点数
#更新出牌后的数据
bidder = turn
playerStr = pCount[turn]
playerStr = playerStr[:cardId] + str(eval(playerStr[cardId]) - nowLen) + playerStr[(cardId + 1):]
pCount[turn] = playerStr
if playerStr == "0000000000000":#当前玩家正好把牌出完了
pList.append(turn + 1)#记录
turn = (turn + 1) % n#轮到下一位玩家
continue
else:#接牌
if turn == bidder:#接自己的牌
times = 0
newId = find_deal(pCount[turn], nowLen, cardId)#检测能否接牌
if newId == -1:#接不了
nowLen = 0
continue
#能接,出牌并更新数据
cardId = newId
playerStr = pCount[turn]
playerStr = playerStr[:cardId] + str(eval(playerStr[cardId]) - nowLen) + playerStr[(cardId + 1):]
pCount[turn] = playerStr
if playerStpList = []#出完牌的列表
turn = 0#当前出牌玩家
nowLen = 0#当前出牌张数(0代表本轮首次出牌)
bidder = 0#上一个出牌的玩家
cardId = 0#上一个出牌的点数
times = 0#仔细读了之后感觉是个flag
while len(pList) < (n - 1):#全部出完牌后退出循环
if turn == bidder:#上一次出牌后无人接牌
times += 1#标记一下
if (turn + 1) in pList:#当前玩家已经出完了
turn = (turn + 1) % n#顺延下一个玩家
continue
print(pCount[turn])
print(cards[cardId], nowLen)
if nowLen == 0:#如果是本轮首次出牌
times = 0
nowLen, cardId = find_max(pCount[turn])#确定出牌的张数和点数
#更新出牌后的数据
bidder = turn
playerStr = pCount[turn]
playerStr = playerStr[:cardId] + str(eval(playerStr[cardId]) - nowLen) + playerStr[(cardId + 1):]
pCount[turn] = playerStr
if playerStr == "0000000000000":#当前玩家正好把牌出完了
pList.append(turn + 1)#记录
turn = (turn + 1) % n#轮到下一位玩家
continue
else:#接牌
if turn == bidder:#接自己的牌
times = 0r == "0000000000000":
pList.append(turn + 1)
turn = (turn + 1) % n
continue
elif times != 0:#上一次出牌后别人接不了,自己也接不了,此轮结束
bidder = turn#最后一次出牌的玩家先出
times = 0
continue
else:#接别人的牌
newId = find_deal(pCount[turn], nowLen, cardId)
if newId == -1:
turn = (turn + 1) % n
continue
times = 0
cardId = newId
bidder = turn
playerStr = pCount[turn]
playerStr = playerStr[:cardId] + str(eval(playerStr[cardId]) - nowLen) + playerStr[(cardId + 1):]
pCount[turn] = playerStr
if playerStr == "0000000000000":
pList.append(turn + 1)
turn = (turn + 1) % n
continue
#输出结果
nSum = round(n * (n + 1) / 2)
for k in pList:
nSum -= k
roadStr = ""
for k in pList:
roadStr += str(k)
roadStr += " "
roadStr += str(nSum)
print(roadStr)
剩下就是工程问题,在脑子里或者纸上模拟出牌过程然后翻译到程序里,每个人都会有不同的处理方式,重点注意边界情况和题目限制即可。自行给程序添加了大部分注释,如果还有不理解的地方请务必留言,这些地方才是备赛最大的意义所在。 |
|