题目如上:
请你用1,3,4,5,6,8,9这7个数字把下列汉子替换下来
----西游记
X ------- 记
大家都爱看
从这要求来分析有如下特征:
1.要求将所有的数字都要用到
2.所有的数字和汉字对应起来,且不能重复
单纯的从数学角度分析较为容易,我门可以从计算机思维上考虑:
1.生成list[1,3,4,5,6,8,9]的三位数排列组合
2.原list的数字位结果的排列组合
3.结果必定是四位数,把三位数的剔除
4.三位数的个位数乘以该三位数,得到的结果需要再“2”种
from itertools import permutations def begin(): s = set() for i in permutations([1, 3, 4, 5, 6, 8, 9], 3): k = '' for j in range(0, len(i)): k = k + str(i[j]) s.add(int(k)) for chengshu in s: # print('=====') listBegin = [1, 3, 4, 5, 6, 8, 9] # chengshu = 986 gewei = chengshu%10; result = chengshu * gewei; list1 = getList(chengshu) list2 = getList(result) # print(list1,list2) set1 = set(list1) set2 = set(list2) setBegin = set(listBegin) resultSet = set1.union(set2) if len(resultSet) == 7: print('=========') if setBegin.issubset(resultSet) and resultSet.issubset(setBegin): print('xxxxxxxx') print(chengshu) print(result) return def getList(num): data1 = int(num%10); data2 = (num // 10)%10; data3 = (num //100)%10; if num//100>=10: data4 = (num//1000)%10 return [data4,data3,data2,data1] return [data3,data2,data1] if __name__ == '__main__': begin()
结果展示:
E:\miniconda\python.exe C:/Users/kiven/Desktop/test/gg_new.py ========= ========= ========= ========= ========= ========= ========= ========= ========= ========= xxxxxxxx 954 3816
很容易可以看出来,西游记三个字分别对应的是954,大家都爱看对应的是3816
大家可以尝试更改这七位数,看看结果又是多少呢?
我这里更改了一下:
**
[2, 3, 4, 5, 7, 8, 9] 其结果位: 547 3829