Python中的快乐跳跃者

2024-06-16 09:34:18 发布

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

我正试图解决卡蒂斯的快乐跳跃问题

下面是问题的链接:https://open.kattis.com/contests/ge97jq/problems/jollyjumpers

下面是我的答案:

values = list(map(int,input().split(" ")))
n = values[0]
x = []

def isJolly(values, n):
    for i in range(1, n-1):
        d = abs(values[i] - values[i+1])

        if (d == 0 or d > n-1 or d in x):
            return False
        else:
            return True
        
if isJolly(values, n):
    print("Jolly")
    
else:
    print("Not Jolly")

我需要帮助,因为Kattis不断返回“错误答案”,我需要找出原因。我认为这与空名单有关


Tags: or答案inhttpsreturnif链接open
1条回答
网友
1楼 · 发布于 2024-06-16 09:34:18

根据定义,如果1..n-1的每一个差异都出现一次,那就太好了。这意味着每个差异都应该是唯一的。 每当我们遇到一个超出范围的差异,或者我们已经看到了,我们就会得出结论,这不是愉快的

def isJolly(values, n):
    x = {0}
    for i in range(1, n-1):
        d = abs(values[i] - values[i+1])

        if (d > n-1 or d in x):
            return False
        else:
            x.add(d)
    return True

相关问题 更多 >