洛谷 题解 P1980 【计数问题】的两种解法
第一种解法第一眼看上去好像比第二中更快,但实际上会超出部分测试的时间
n,x = map(int,input().split()) count= 0 a=[] for m in range(1,n+1): i =m while i >= 1: if i % 10 == x: count+=1 i=int(i/10) print(count)
而第二种将每个元素转化为了字符串,节省了运算时间
n,x = map(int,input().split()) x = str(x) count= 0 a=[] for i in range(1,n+1): a.append(str(i)) for i in range(0,n): for m in a[i]://遍历a[i]字符串中每个字符 for n in m: if n == x: count+=1 print(count)