内个,题目不是说吗?
n = 3,k =7 时,意思不就是 7 个数的和要等于 3 吗?确实不存在啊……
呃,抱歉,搞错了…… from itertools import permutations
def fun250(k,n):
nums =
result = []
for i in permutations(nums,3):
if sum(i) == n and sorted(i) not in result:
result.append(sorted(i))
return result from itertools import permutations as pt
def func(k,n):
per = pt(range(1,10),k)
count = []
for each in per:
each = list(each)
each.sort()
if sum(each) == n:
if each not in count:
count.append(each)
return count
print(func(3,7)) 永恒的蓝色梦想 发表于 2019-10-1 12:35
恭喜通过!
执行用时:40 ms # 找出所有相加之和为 n 的 k 个数的组合。/
# 组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
gs = int(input('请输入次数:'))
he = int(input('请输入和:'))
while True:
if he>24:
he = int(input('超出和的最大值,请重新输入:'))
else:
break
jihe = []
for a in range(1,10):
for b in range(1,10):
for c in range(1,10):
if a!=b!=c and a!=c and a+b+c==he:
jihe.append()
if len(jihe)==0:
print('没有要求的组合。')
elif len(jihe)<=gs:
for i in range(len(jihe)):
print(jihe)
else:
for i in range(gs):
print(jihe) 好像没有完成第二个要求 from itertools import combinations as c
def f250(k,n):
L=[]
for i in c(range(1,10),k):
if sum(i)==n:
L.append(list(i))
return L
print(f250(3,9))
页:
1
[2]