## 用python猜测一个数字是集合里面哪些数字相加求和而来的

Python 一个 数字 猜测 个数

```import itertools
import math
abc=22.8#10.8,22,10
list_abc=[
1.82,0.82,1.6,7.41,4.94,7.6,21.97,82.14,25.8,45.89,20.09,
16.81,0.08,0.02,1.6,7.85,6.44,0.04,63.01,0.82,9.11,1.39
]
deep=20#控制计算深度
def hwj_cha(part,all):#唯一匹配差集 多个相同集合只清理一次
res=all.copy()
for x in part:
res.remove(x)
return tuple(res)
temp_list = []#只筛选小于的数据
for dl in list_abc:
if (dl < abc):
temp_list.append(dl)
print(abc)
print(temp_list)
print("集合长度="+str(len(temp_list)))
print("----------开始计算可能性-----------")
list_abc_sum=round(sum(temp_list),2)
list_abc_hlen=math.floor(len(temp_list)/2)#只算到一半长度
deep=list_abc_hlen if list_abc_hlen<deep else deep#计算深度控制参数
deep+=1#循环只执行到范围内，需要增加1
for i in range(0,deep):
temp = list(itertools.combinations(temp_list, i))
okw = False
for j in temp:
thesum1=round(sum(j),2)
thesum2=round((list_abc_sum-thesum1),2)
if (thesum1==abc or thesum2==abc):#如果两个猜测正确
if (thesum1 == abc):
pipei=j
sumok=thesum1
else:
pipei=hwj_cha(j,temp_list);
sumok=thesum2
print(str(i)+"#计算成功：")
print(str(sumok)+"=sum"+str(pipei))
okw = True
#break #跳出对比，如果本轮只做一次对比
#if(okw):break #跳出对比，如果全部只做一次对比```

运算结果如下:

```22.8
[1.82, 0.82, 1.6, 7.41, 4.94, 7.6, 21.97, 20.09, 16.81, 0.08, 0.02, 1.6, 7.85, 6.44, 0.04, 0.82, 9.11, 1.39]

----------开始计算可能性-----------
5#计算成功：
22.8=sum(0.82, 4.94, 0.08, 7.85, 9.11)
5#计算成功：
22.8=sum(4.94, 0.08, 7.85, 0.82, 9.11)
6#计算成功：
22.8=sum(1.82, 0.82, 1.6, 1.6, 7.85, 9.11)
6#计算成功：
22.8=sum(1.82, 1.6, 1.6, 7.85, 0.82, 9.11)
7#计算成功：
22.8=sum(0.82, 4.94, 0.08, 0.02, 6.44, 9.11, 1.39)
7#计算成功：
22.8=sum(4.94, 0.08, 0.02, 6.44, 0.82, 9.11, 1.39)
8#计算成功：
22.8=sum(1.82, 0.82, 1.6, 0.02, 1.6, 6.44, 9.11, 1.39)
8#计算成功：
22.8=sum(1.82, 1.6, 0.02, 1.6, 6.44, 0.82, 9.11, 1.39)
9#计算成功：
22.8=sum(1.82, 0.82, 1.6, 7.6, 0.08, 1.6, 7.85, 0.04, 1.39)
9#计算成功：
22.8=sum(1.82, 1.6, 7.6, 0.08, 1.6, 7.85, 0.04, 0.82, 1.39)
9#计算成功：
22.8=sum(1.82, 1.6, 7.6, 0.08, 1.6, 7.85, 0.04, 0.82, 1.39)
9#计算成功：
22.8=sum(1.82, 1.6, 7.6, 0.08, 1.6, 7.85, 0.04, 0.82, 1.39)```

https://www.cnblogs.com/prefertea/p/13952302.html