用1-8八个数字不重复使用,组成两个四位数,其中一个是另一个的四倍。
#encoding:utf8 from itertools import * def delSrc(i, l): l.pop(l.index(i)) return l def makeStr(l): s = "" for i in l: s += str(i) return s if __name__ == '__main__': num = [1,2,3,4,5,6,7,8] for i in combinations(num, 4): l = num for j in i: l = delSrc(j,list(l)) str1, str2 = makeStr(i), makeStr(l) p, q = list(permutations(str1)), list(permutations(str2)) for m in p: for n in q: s1,s2 = "","" for a, b in zip(m,n): s1 +=a s2 +=b if int(s1) == 4 * int(s2) or int(s2) == int(s1): print s1, s2