首页 > 解决方案 > 提交时它给出 runtime_error

问题描述

t=int(input())

while t>0 :

    c=0

    n,h,y1,y2,e = list(map(int, input().split()))
    for i in range(n):

        x0,x1 = list(map(int, input().split()))    
        if x0==1 :
            if x1 < h-y1:
                e -= 1
        else :
            if y2 < x1 :
                e -= 1
        if e>0 :
            c+=1    
        else :
            break
    print(c)

    t-=1

它正在通过示例测试用例,但在提交时,它显示出现运行时错误(NZEC)。

这是问题的链接:https ://www.codechef.com/problems/PIPSQUIK

标签: runtime-error

解决方案


问题是您正在读取输入并同时处理它们。因此,在某些测试用例中可能会出现这样的情况,e<=0但您仍然需要x0 x1阅读一些内容(即i<n-1)。在这种情况下,您将break进入循环,因为e<=0在循环的下一次迭代中while,您将尝试读取 5 个值n,h,y1,y2,e = list(map(int, input().split())),但您只会收到 2 个值x0 x1,因此它会抛出 a ValueError: not enough values to unpack (expected 5, got 2),因此它不会通过所有测试案例。

要解决此问题,只需先获取所有输入,然后根据您当前的逻辑处理它们。

t=int(input())

while t>0 :

    c=0

    n,h,y1,y2,e = list(map(int, input().split()))
    inputs = []
    for i in range(n):
        inputs.append(list(map(int, input().split())))
    for inp in inputs:

        x0,x1 = inp
        if x0==1 :
            if x1 < h-y1:
                e -= 1
        else :
            if y2 < x1 :
                e -= 1
        if e>0 :
            c+=1
        else :
            break
    print(c)

    t -= 1

推荐阅读