我正在尝试解决下一个问题:当我在我的终端中运行代码时,它与练习中给出的示例一起工作;但是当我在CodeForces中提交它时,它不会导致一个运行时错误,而我无法理解它。
def increment(arr, length, option, add):
for i in range(length):
if(option == '0') and (arr[i]%2 == 0):
arr[i] += add
elif(option == '1') and (arr[i]%2 != 0):
arr[i] += add
else:
pass
return arr
def main():
quantityOperation = int(input())
while quantityOperation > 0:
length, times = input().split()
length = int(length)
times = int(times)
arr = [int(x) for x in input().split()]
while times > 0:
opt, add = input().split()
add = int(add)
res = sum(increment(arr, length, opt, add))
print(res)
times -= 1
quantityOperation -= 1
main()发布于 2022-10-24 12:14:52
主函数内的循环没有结束。您应该将quantityOperation -= 1放在while循环中。
但是,您的代码在修复后仍然会超过时间限制。正确的思路是预先计算奇偶和,并根据查询对其进行修改,而不是每次循环增量函数中的所有元素。您可以检查社论是否有此问题。
https://stackoverflow.com/questions/74160921
复制相似问题