如何解决在Codechef中获得错误答案领先游戏
输入的第一行将包含一个整数N(N≤10000),指示游戏中的回合数。一旦所有回合都结束,则宣布在游戏中任一回合结束时领先优势最大的玩家为获胜者。您的输出必须由包含两个整数W和L的单行组成,其中W为1或2并表示获胜者,L是获胜者获得的最大领先优势。 当我在codechef中提交此答案时,我得到了错误答案。我不知道为什么谁能帮我找出我的代码出了什么问题? 例子
输入:
5
140 82
89 134
90 110
112 106
88 90
Output:
1 58
testcases = int(input())
lead = 0
for i in range(testcases):
player1,player2 = map(int,input().split())
if abs(player1 - player2) >= abs(lead):
lead = player1-player2
if lead>0:
w = 1
else:
w = 2
print(w,abs(lead))
解决方法
在“领先游戏”中,每个领先者都是由得分的累计差异决定的,但是在这里您会发现每个新得分都有领先者,这是错误的。
使用此代码:
testcases = int(input())
#cumulative score for player 1 and 2
c1=0
c2=0
max_lead=0
for i in range(testcases):
player1,player2 = map(int,input().split())
# determine the cumulative score
c1+=player1
c2+=player2
if c1>c2:
lead = c1-c2
if lead > max_lead:
max_lead = lead
w=1
else:
lead = c2-c1
if lead > max_lead:
max_lead = lead
w=2
print(w,max_lead)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。