首页 > 解决方案 > 为什么我的 prgram 在 Visual Studio 中没有时在 codechef ide 中显示运行时错误?

问题描述

A给定一个大小N为整数的数组K,检查是否存在任何一对索引ij例如A[i] + A[j] = Ki ≠ j。第一行应该包含测试用例的数量,第二行应该包含输入NK。我编码但它显示运行时错误。我该如何解决?代码如下。

我已经在 Visual Studio 中运行了该程序,它工作正常,但是当我提交给 codechef 时,它显示运行时错误。

#following is my code where T is number of test cases
T = int(input())

while T != 0:
    #size of array and value of K ,also the array
    N, K = map(int, input().split())
    A = list(map(int, input().split()))     
    l = 0
    r = N - 1
    flag = 0
    while l < r:
        if (A[l] + A[r] == K):
            flag = 1
            break
        elif (A[l] + A[r] < K):
            l += 1
            continue
        else:
            r -= 1 
            continue

    if flag == 1:
        print("Yes", end="\n")
    else:
        print ("No", end="\n")

代码是否打印Yes取决于数组中是否K存在 sum。

标签: python

解决方案


起初,您的算法仅在输入数组已排序时才有效。此外,您的解决方案T永远不会改变,因此T != 0条件始终为真并导致无限循环!

这是我接受的这个问题的解决方案。让我们检查一下!

t = int(input())

for _ in range(t):
    n, k = map(int, input().split())
    arr = list(map(int, input().split()))

    found = False
    for i, a in enumerate(arr):
        for j, b in enumerate(arr):
            if i != j and a + b == k:
                found = True

    print('Yes' if found else 'No')

CodeChef IDE 上,当我尝试Run时,会导致Runtime Error

在此处输入图像描述

但是当我尝试使用自定义输入运行时,它起作用了。所以我决定提交我的代码并被接受!因此,我认为CodeChef IDE 有问题,建议您只提交代码。


推荐阅读