2020-/National_Java_B/A
小蓝特别喜欢 2,今年是公元 2020 年,他特别高兴。
他很好奇,在公元 1 年到公元 2020 年(包含)中,有多少个年份的数位中包含数字 2?
解题思路:
for循环遍历,将数字类型变为字符串型,判断。
代码:
count = 0
for i in range(1, 2021):
string = str(i)
if '2' in string:
count += 1
print('有{}个年份的数位中包含数字2'.format(count))
运行结果: