今天的主角是数字9。给定一个正整数n,请编写一个函数,统计从数字1到n(包含)中出现了多少个9。例如99,919都算有2个9。
【示例】
输入:10
输出:1
解释:数字1到10中,只有数字9满足条件,所以算作1个。
题目难度:中等
题目来源:codewars-count '9’s from 1 to n
def solution(n: int)-> int:
# your code here
assert solution(8) == 0
assert solution(10) == 1
assert solution(100) == 20
assert solution(279) == 48
# 进阶
assert solution(99999) == 50000
assert solution(565754) == 275645