刚也发现了,偶数个数的情况。都是先手胜或和平局,所以代码可以优化一下偶数的情况,,当然这次不严谨,有些平局我就默认先手胜A_player,B_player=0,0
def fun352(lst):
if not len(lst):return '平局'
elif len(lst)==1 and lst[0]>0:return True
elif len(lst)==1 and lst[0]==0:return '平局'
elif not len(lst)%2:
if len(set(lst))==1:return '平局'
else:return True
else:
def recursive(lst,player,i):
global A_player,B_player
if len(lst)==1 and i%2:
B_player+=lst[0]
return
elif len(lst)==1 and not i%2:
A_player+=lst[0]
return
if lst[0]>=lst[1] and lst[-1]>=lst[-2]:
if lst[0]>=lst[-1]:
player+=lst[0]
lst.pop(0)
else:
player+=lst[-1]
lst.pop(-1)
elif lst[0]>=lst[1] and lst[-1]<lst[-2]:
player+=lst[0]
lst.pop(0)
elif lst[0]<lst[1] and lst[-1]>=lst[-2]:
player+=lst[-1]
lst.pop(-1)
else:
if (lst[1]-lst[0])>=(lst[-2]-lst[-1]):
player+=lst[-1]
lst.pop(-1)
else:
player+=lst[0]
lst.pop(0)
i+=1
if i%2:
A_player+=player
recursive(lst,0,i)
else:
B_player+=player
recursive(lst,0,i)
recursive(lst,0,0)
return True if A_player>B_player else '平局' if A_player==B_player else False
|