为什么下面的python代码会抛出内存错误?

进入阿比

这是代码:

import itertools

num_cases = int(input())

answer_list = []

while num_cases>0:
    live_ans = []
    question_list = []
    nums = int(input())
    d1 = int(input())
    d2 = int(input())
    sample_space= {d1, d2}
    temp = []

    no_cases = 2**(nums-1)

    combs = itertools.product(sample_space, repeat = nums-1)

    for i in combs:
        temp.append(i)

    for i in temp:
        if sum(i) not in live_ans:
            live_ans.append(sum(i))
        else:
            pass
    live_ans.sort()
    answer_list.append(live_ans)
    num_cases -= 1

for i in answer_list:
    finalans = " ".join(map(str, i))
    print(finalans)

对于小输入,如:

1
3
1
2

该程序运行良好。至于相对较大的输入,如:

1
58
69
24

它给出了一个内存错误。我没有为此引用任何原因,因为代码看起来根本不消耗内存。不是吗?

ycx

看看你的以下几行:

no_cases = 2**(nums-1)
combs = itertools.product(sample_space, repeat = nums-1)
for i in combs:
    temp.append(i)

2**58 = 2.8823038e+17
你计算一下为什么从这里得到内存错误

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么下面的代码会抛出错误?

为什么下面的代码会产生分段错误

为什么代码会抛出非法内存访问错误

为什么下面的 TypeScript 程序不会抛出类型错误?

在下面的代码中,为什么会提示“OUCH”?

为什么下面的代码会导致excel崩溃?

为什么下面的Parse代码没有返回错误?

为什么下面的Java代码会给出StackOverflow错误?

为什么下面的代码编译没有错误?

为什么下面的代码有语法错误?

为什么下面的python代码不起作用?

为什么下面的程序会死锁?

为什么Python会抛出此错误?

为什么下面的代码使用Iterator next()和remove()抛出ConcurrentModificationException?

为什么下面的代码显示NameError?

为什么下面的代码给ArrayIndexOutofBounds异常?

为什么下面的代码输出是这样的

如果条件抛出错误,为什么下面的负数呢?

下面的代码会导致内存泄漏吗?

为什么我的代码会抛出错误消息?

为什么我的 SQL 代码会抛出转换错误?

下面的python代码的输出是什么

为什么下面的代码编译时没有错误?

为什么我没有收到任何错误在下面的代码?

为什么链接器在下面的代码中没有发出错误?

为什么下面的代码片段没有给出编译时错误?

为什么在运行下面的代码时会出现stackoverflow错误?

下面的两个go代码有什么区别,为什么要使用这么多不同的内存

为什么下面的程序会进行无限循环?