google codejam显示python运行时错误

2024-10-02 00:34:36 发布

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

这段代码是为GoogleCodeJam竞赛编写的。下面的代码在我的电脑上正确编译,并给出了示例代码的正确结果。然而,每当我试图在谷歌网站上运行它时,它就会显示运行时错误。我把它弄乱了一个小时,仍然不知道它出了什么问题

def reversort(reverList):
    global totalScore
    length = len(reverList)
    score = 0

    for i in range(length - 1):
        minimum = reverList.index(min(reverList[i: ]))
        tempList = reverList[i:minimum + 1]
        tempList.reverse()
        reverList[i: minimum + 1] = tempList
        score += minimum - i + 1
    
    totalScore.append(score)

if __name__ == "__main__":
    t = int(input())
    totalScore = []
    rev = []

    for i in range(t):
        n = int(input())
        apen = []
        for j in range(n):
            apen.append(int(input()))
        reversort(apen)
        print("Case #{}: {}".format(i+1,totalScore[i]))
        rev.append(apen)

Tags: 代码inforinputrangelengthintscore
1条回答
网友
1楼 · 发布于 2024-10-02 00:34:36

试试这个

reverse = int(input())
for i in range(1, reverse + 1):
a = int(input())
b = list(map(int, input().split()))
out = 0
for index in range(a-1):
    min_index = b.index(min(b[index:a]))
    b[index: min_index + 1] = reversed(b[index: min_index + 1])
    out += (min_index) - (index) + 1
print("Case #{}: {}".format(i, out))

相关问题 更多 >

    热门问题