首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CODEFORCES 1744 B,如何修复运行时错误?

CODEFORCES 1744 B,如何修复运行时错误?
EN

Stack Overflow用户
提问于 2022-10-22 03:44:08
回答 1查看 51关注 0票数 0

我正在尝试解决下一个问题:当我在我的终端中运行代码时,它与练习中给出的示例一起工作;但是当我在CodeForces中提交它时,它不会导致一个运行时错误,而我无法理解它。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2022-10-24 12:14:52

主函数内的循环没有结束。您应该将quantityOperation -= 1放在while循环中。

但是,您的代码在修复后仍然会超过时间限制。正确的思路是预先计算奇偶和,并根据查询对其进行修改,而不是每次循环增量函数中的所有元素。您可以检查社论是否有此问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74160921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档