关闭Tweets CodeCh

2024-04-20 14:01:30 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能告诉我为什么我要在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

Tags: incomfalseforinputifcountrange
1条回答
网友
1楼 · 发布于 2024-04-20 14:01:30
for i in range(0, l):
    tweets.append(False) 

这是错误的,有N条微博,我只是点击量。你知道吗

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单击tweets都是很好的,但是如果你想单击tweet 21,你只是选择最后一个字符,所以你单击tweet 1而不是21。按空格分割并选择所有数字将是解决问题的一种方法

相关问题 更多 >