代码如下:
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
它给出了一个内存错误。我没有列举任何原因,因为代码看起来根本不占用内存。不是吗?
答案 0 :(得分:3)
查看以下几行:
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
您可以算出为什么它会从这里得到内存错误