AcWing 338. 计数问题
原题链接
中等
作者:
rebornczs
,
2019-10-08 20:43:45
,
所有人可见
,
阅读 612
题目描述
注意:本题非动态规划,直接分析(分情况讨论) 主要就是把0拉出来讨论,自己写个例子例如113试一下就知道为什么遇到0要减1
def count(n, x):
tmp = n
res = 0
base = 1
st = False
while tmp:
mod = tmp % 10
tmp //= 10
if x == 0:
if tmp:
tmp -= 1
st = True
else: break
if mod == x:
res += n % base + 1
elif mod > x:
res += base
res += tmp * base
base *= 10
if st:
tmp += 1
st = False
return res
if __name__ == "__main__":
ans = [0] * 10
while True:
a, b = map(int, input().split())
if a == 0 and b == 0:
break
if a > b: a, b = b, a
for i in range(10):
ans[i] = count(b, i) - count(a - 1, i)
print(" ".join(map(str, ans)))
这题数据大点不就数位dp的模板题吗