python - 为什么我的 prgram 在 Visual Studio 中没有时在 codechef ide 中显示运行时错误?
问题描述
A
给定一个大小N
为整数的数组K
,检查是否存在任何一对索引i
,j
例如A[i] + A[j] = K
和i ≠ j
。第一行应该包含测试用例的数量,第二行应该包含输入N
和K
。我编码但它显示运行时错误。我该如何解决?代码如下。
我已经在 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。
解决方案
起初,您的算法仅在输入数组已排序时才有效。此外,您的解决方案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 有问题,建议您只提交代码。
推荐阅读
- github - Sourcetree 问题:目标目录的问题
- excel - 在 Excel 中删除以“(”开头的单词
- java - Spring中如何设置@NotEmpty注解消息?
- java - 使用分隔符读取文件并添加到数组
- git - Windows Portable Git 与 Portable Visual Studio Code 的集成:使用哪个 git.exe?
- python - Boost::Python 断言 `Py_REFCNT(m_ptr) > 0' 失败
- javascript - 使用复制粘贴角度上传文件
- angular - ngModel 值未反映 @angular-material-components/datetime-picker 的手动输入和自定义格式
- html - 以`?`开头的Achor标签:Django分页:下一个/上一个链接需要一个索引
- prometheus - 在普罗米修斯上构建重划线图