获取最小二段数
题面
一个任意的正整数n,求出比n大并且是n的倍数的最小二段数
二段数是这样的正整数:恰好包含两种不同的十进制数字s和t,s不是0,并且s的所有出现均排列在所有的t的前面
例如,44444411是二段数(s是4,t是1),41、10000000和5555556也是。
但4444114和44444都不是二段数。
分析
- 正则判断二段数
- 倍数查找最小跳出循环
代码
import re
# 判断二段数
def Dis(s):
if s[0] == s[-1]:
return False
p = r'^([123456789])\1*(\d)\2*$'
return False if re.match(p, s, flags=0) is None else True
# 找出最小二段敉
def find(n):
i = 1
while True:
bs = n*i
bs = 10 if bs < 10 else bs
if Dis(str(bs)):
return (n, bs)
i += 1
arr = []
while True:
n = int(input())
if n == 0:
break
arr.append(n)
for i in arr:
print("%d: %d" % find(i))
输出
(base) D:\code-base\python\test>python -u "d:\code-base\python\test\bin2.py"
1
2019
0
1: 10
2019: 9999999993
本作品采用《CC 协议》,转载必须注明作者和本文链接
这样好像比正则式快一些