线上OJ:
一本通:http://ybt.ssoier.cn:8088/problem_show.php?pid=1961
核心思想:
本来想找规律,后来发现本题的数据范围不大,n为 1 0 6 10^6 106,即使每一位都判断一次,最坏的情况下时间复杂度也仅为 O ( 7 ∗ 1 0 6 ) O(7*10^6) O(7∗106),故本题可以考虑直接枚举,不需要去找数字上的规律了。
题解代码:
#include <bits/stdc++.h>
using namespace std;int n, x, ans=0;int main()
{cin >> n >> x;for(int i = 1; i <= n; i++){int tmp = i; // 这里需要用tmp代替i,因为i要++,i不能变while(tmp) {if((tmp % 10) == x) ans++; // 如果最后一位是x,则ans++tmp = tmp / 10; // 去除个位数字}}cout << ans << endl;return 0;
}