在codejam中获取运行时错误,但在其他地方工作正常



我用python 3编写了代码。它在python ide和programiz online ide上运行得很好,但在谷歌平台上会出现RUNTIME错误。有人能解释为什么会发生这种事吗?这是问题的链接:残留

def main(x,n,N):
n = len(x)
r = 0
for i in x:
if len(i)> len(set(i)):
r += 1
cols = []
k = 0
for i in range(n):
col = []
for j in range(n):
col.append(x[i][j])
if i == j:
k += int(x[i][j])
cols.append(col)
c = 0
for i in cols:
if len(i)> len(set(i)):
c += 1
print("Case #",N,":",k,r,c,)


N = int(input())
for i in range(N):
x = []
n = int(input())
for j in range(n):
row = []
for p in range(n):
v = int(input())
row.append(v)
x.append(row)
num = i + 1
main(x,n,num)
  1. 您处理输入不正确。您为一行中的每个元素调用input(),这会给出一个错误。您应该将整行作为一个输入进行处理。替换
for j in range(n):
row = []
for p in range(n)
v = int(input())
row.append(v)
x.append(row)

for j in range(n):
row = list(map(int, input().split()))
x.append(row)
  1. 尝试在IDE中运行此语句:
print("Case #",1,":",1)

这将打印Case # 1 : 1。程序明确规定输出为Case #1: 1格式。#和案例编号之间的多余空间会导致错误。使用Python3及以上版本时,请使用f-string使其更容易。

print(f"Case #{N}: {k} {r} {c}")

这将修复运行时错误。我仍然怀疑它会给出一个错误的答案。

相关内容

最新更新