题解 | 记数问题
记数问题
https://www.nowcoder.com/practice/28b2d9f2bf2c48de94a1297ed90e1732
# range循环打印 1~n ,n转化成字符串,存入列表中? 检测到有x就加一
# n,x = map(int,input().split())
# result = 0
# for i in range(1,n+1):
# i = str(i)
# for j in i:
# if j == str(x):
# result += 1
# else:
# continue
# print(result)
# 以上未在时间限制内给出答案,pass
# 采用数位法:
n,x = map(int,input().split())
result = 0
for i in range(1,n+1):
while i:
if i % 10 == x:
result += 1
i = i // 10
print(result)
有意思的是 n % 10 永远得到n最右边的数,n // 10 可以消除n最右边的数,两个结合,可以直接遍历到任何整数n 的所有位
难题,有启发的题 文章被收录于专栏
记录个人学习中的代码,刷代码遇到的有意思的题

查看16道真题和解析