gpt4 book ai didi

python - 关闭推文 - CodeChef

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:14:12 26 4
gpt4 key购买 nike

有人可以告诉我为什么我会因为 CodeChef 上的以下解决方案而获得 WA 吗?

问题链接:https://www.codechef.com/problems/TWTCLOSE

解决方法:

n, k = map(int, input().split()) 
com = []
while(k):
k -= 1
com.append(input())
l = len(com)
tweets = []
for i in range(0, n):
tweets.append(False)
for i in range(0, l):
if(com[i] == "CLOSEALL"):
for j in range(0, n):
tweets[j] = False
else:
temp = com[i]
tweets[int(temp[-1])-1] = not tweets[int(temp[-1])-1]
count = 0
for i in range(0, n):
if(tweets[i]):
count += 1
print(count)

输入:

3 6
CLICK 1
CLICK 2
CLICK 3
CLICK 2
CLOSEALL
CLICK 1

输出:

1
2
3
2
0
1

最佳答案

for i in range(0, l):
tweets.append(False)

这是错误的,有N条推文,l只是点击量。

if(com[i] == "CLOSEALL"):
for j in range(0, l):
tweets[j] = False

出于同样的原因错误。

for i in range(0, l):
if(tweets[i]):
count += 1

同样,只计算到 l 而不是 N。事实上,您根本不会使用 n。这是一个很大的暗示,你错过了一些东西。

更正后更新:

tweets[int(temp[-1])-1] = not tweets[int(temp[-1])-1]

temp[-1] 表示最后一个字符,这对于点击推文 1 到 9 都很好,但是如果你想点击推文 21,你只是选择最后一个字符,所以你点击推文 1 而不是 21。按空格拆分并选择所有数字将是修复它的一种方法

关于python - 关闭推文 - CodeChef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51363584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com