Python练习题:有一个6位数,它的2倍、3倍、4倍、5倍、6倍都由原数的6个数字组成,只是顺序不同,求这个6位数
题目:有一个6位数,它的2倍、3倍、4倍、5倍、6倍都由原数的6个数字组成,只是顺序不同,求这个6位数。
#!/usr/bin/python3
# -*- encoding:utf-8 -*-
#检查每一位数字是否存在于倍数结果中
def check(result,num):
length = len(num)
count = 0
for i in range(length):
if str(num[i]) in str(result):
count +=1
else:
break
return count
#循环六位数
for i in range(100000,999999):
#倍数结果
two = i * 2
three = i * 3
four = i * 4
five = i * 5
six = i * 6
#抽取每位数字存到列表中
list = []
a = i % 10
b = int( i / 10 %10 )
c = int( i / 100 %10 )
d = int( i / 1000 %10 )
e = int( i / 10000 %10 )
f = int( i / 100000 %10 )
list.append(a)
list.append(b)
list.append(c)
list.append(d)
list.append(e)
list.append(f)
#判断是否有数字重复
if a == b or b == c or c == d or d == e or e == f:
pass
elif check(two,list) == 6 and check(three,list) == 6 and check(four,list) == 6 and check(five,list) == 6 and check(six,list) == 6:
print("{} is ok".format(i))
else:
pass
更优解:
#!/usr/bin/python3
# -*- encoding:utf-8 -*-
#检查每一位数字是否存在于倍数结果中
def check(num):
origlist = set(list(str(x)))
x2list = set(list(str(x * 2)))
x3list = set(list(str(x * 3)))
x4list = set(list(str(x * 4)))
x5list = set(list(str(x * 5)))
x6list = set(list(str(x * 6)))
if origlist == x2list == x3list == x4list == x5list == x6list:
return True
return False
'''
print(origlist)
print(x2list)
print(x3list)
print(x4list)
print(x5list)
print(x6list)
'''
for x in range(100000,200000):
if check(x):
print(x)
本文来自博客园,作者:酒粒,转载请注明原文链接:https://www.cnblogs.com/charliewch/p/16481857.html