📅  最后修改于: 2023-12-03 15:28:13.934000             🧑  作者: Mango
这个问题考察的是算术能力和编程实现能力。给定两个正整数N和K,编写一个程序来求N中包含K的数字的个数。
def count_k(n: int, k: int) -> int:
"""
计算n中包含k的数字的个数
:param n: 正整数N
:param k: 正整数K
:return: 包含k的数字的个数
"""
count = 0
str_n = str(n)
for c in str_n:
if int(c) == k:
count += 1
return count
assert count_k(12345, 5) == 1
assert count_k(54321, 5) == 1
assert count_k(55555, 5) == 5
assert count_k(101010, 0) == 2