【问题标题】:Getting Runtime error in codejam but works fine elsewhere在 codejam 中出现运行时错误,但在其他地方工作正常
【发布时间】:2021-03-23 11:47:52
【问题描述】:

我在 python 3 中编写了我的代码。它在 python ide 和 programiz online ide 上运行良好,但在 google 平台上出现 RUNTIME 错误。有人可以帮忙解释为什么会这样吗? 这是问题的链接Vestigium

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)

【问题讨论】:

    标签: python python-3.x algorithm error-handling google-code


    【解决方案1】:
    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 格式。 # 和案例编号之间的额外空格会导致错误。使用 Python 3 及更高版本,使用 f-string 更容易。

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

    这应该可以修复运行时错误。我仍然怀疑它会给出错误的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 2016-12-08
      相关资源
      最近更新 更多