题解
【基础】数的统计
3 条题解
-
0
#include <bits/stdc++.h> using namespace std; int main(){ int n, x, cnt = 0;//1~n之间x这个数出现了多少次,cnt计数器 cin >> n >> x; //扫描1~n之间的所有数 for (int i = 1; i <= n; i++) { //对i扫描到的数进行拆位 int t = i;//不可以直接拆i会影响循环 while (t != 0) {//一直拆,拆到t == 0 if (t % 10 == x) {//找到和x一样的数就将计数器+1 cnt++; } t /= 10;//千万别忘了除以10,否则无限循环 } } cout << cnt; return 0; }
- 1