gpt4 book ai didi

python - 形成列表中可能的最大数字

转载 作者:太空狗 更新时间:2023-10-30 01:04:32 25 4
gpt4 key购买 nike

<分区>

给定一个列表,例如:[3, 30, 34, 5, 9]。输出:9534330编写程序返回尽可能大的数

在我的代码中,我在这里使用了排列:

from itertools import permutations
x = [3, 30, 34, 5, 9]
y = permutations(x)
n = len(y)
e = []
for i in y:
a = map(str, i)
e.append(int("".join(i)))
print "Largest Number {}".format(sorted(e)[-1])

这里 n 是排列数的长度,因为 5!,所以是 120。有没有更好的方法来解决这个问题?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com