题目2:N,K --第一行输入数字N和K值,用空格间隔如【5 5】第二行输入N个数字【2 8 12 1 4】---取数8+12+1+4=25是5的5倍2 8 12 1 4
#去掉中间输出,简化(2022-6-28)-END版本
nk=input().split(" ")
n=int(nk[0])
k=int(nk[1])
a=input().split(" ")
a_list=[int(i) for i in a ]
a_sum=[]#放和
qushu_list=[]#放由那些数字组合
for i in range(n):#开始位置
for j in range(i+1,n+1):#从哪个位置切片,n+1,后面的sum取到0,保留列表中的每个字段
for m in range(n-i):#切片用,切m个数字
t=a_list[i]+sum(a_list[(j):][:m])#和
q=[a_list[i]]+a_list[(j):][:m]#取数的列表
if q not in qushu_list:
qushu_list.append(q)#下标不一样才放入下标list
a_sum.append(t)
print("和",a_sum)#考试时不用
print("数字组合",qushu_list)#考试时不用
beishu=[]
for i in a_sum:
if i%k==0:
b=i/k
beishu.append(b)
b_max=max(beishu)
if beishu is []:
print(-1)
else:
for i in range(len(a_sum)):
if b_max*k==a_sum[i]:#取下标
for j in qushu_list[i]:
print(j,end=" ")
输出行为:
8 8 1 2 3 4 5 6 7 8 和 [1, 3, 6, 10, 15, 21, 28, 36, 4, 8, 13, 19, 26, 34, 5, 10, 16, 23, 31, 6, 12, 19, 27, 7, 14, 22, 8, 16, 9, 2, 5, 9, 14, 20, 27, 35, 6, 11, 17, 24, 32, 7, 13, 20, 28, 8, 15, 23, 9, 17, 10, 3, 7, 12, 18, 25, 33, 8, 14, 21, 29, 9, 16, 24, 10, 18, 11, 4, 9, 15, 22, 30, 10, 17, 25, 11, 19, 12, 5, 11, 18, 26, 12, 20, 13, 6, 13, 21, 14, 7, 15, 8] 数字组合 [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7, 8], [1, 3], [1, 3, 4], [1, 3, 4, 5], [1, 3, 4, 5, 6], [1, 3, 4, 5, 6, 7], [1, 3, 4, 5, 6, 7, 8], [1, 4], [1, 4, 5], [1, 4, 5, 6], [1, 4, 5, 6, 7], [1, 4, 5, 6, 7, 8], [1, 5], [1, 5, 6], [1, 5, 6, 7], [1, 5, 6, 7, 8], [1, 6], [1, 6, 7], [1, 6, 7, 8], [1, 7], [1, 7, 8], [1, 8], [2], [2, 3], [2, 3, 4], [2, 3, 4, 5], [2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [2, 4], [2, 4, 5], [2, 4, 5, 6], [2, 4, 5, 6, 7], [2, 4, 5, 6, 7, 8], [2, 5], [2, 5, 6], [2, 5, 6, 7], [2, 5, 6, 7, 8], [2, 6], [2, 6, 7], [2, 6, 7, 8], [2, 7], [2, 7, 8], [2, 8], [3], [3, 4], [3, 4, 5], [3, 4, 5, 6], [3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8], [3, 5], [3, 5, 6], [3, 5, 6, 7], [3, 5, 6, 7, 8], [3, 6], [3, 6, 7], [3, 6, 7, 8], [3, 7], [3, 7, 8], [3, 8], [4], [4, 5], [4, 5, 6], [4, 5, 6, 7], [4, 5, 6, 7, 8], [4, 6], [4, 6, 7], [4, 6, 7, 8], [4, 7], [4, 7, 8], [4, 8], [5], [5, 6], [5, 6, 7], [5, 6, 7, 8], [5, 7], [5, 7, 8], [5, 8], [6], [6, 7], [6, 7, 8], [6, 8], [7], [7, 8], [8]] 2 4 5 6 7 8