更新:输入的数字> 44超时。关于如何阻止其超时的任何想法? 我的代码应该输出最小的数字,该数字仅包含输入分为的1或0。在此示例中,仅包含1或0的34的最小倍数是111010。但是,我的代码仅输出无限循环...有什么想法吗?
#example input: 34
#example ouput: 111010
#2<n<1000
print("Please enter your number...")
n = int(input())
counter = 0
mylist = ['2','3','4','5','6','7','8','9']
t = n
check = str(t)
while any(x in check for x in mylist):
counter += 1
t = n * counter
continue
else:
print(t)
答案 0 :(得分:1)
简单地将给定数字加到一个和上,直到它的字符串仅包含0和1:
# you already solved the input part, so skipping it here
n = 34
summed = n
allowed = {"0","1"}
while set(str(summed)) - allowed: # @MadPhysicists suggestion instead of
# any(x not in allowed for x in str(summed)):
summed += n
else:
print(summed)
输出:
111010
不知道,但是从长远来看,我猜是加法比乘法快。