Files
python/TangDou/AcWing/ShuWeiDP/338_BruteForce.cpp

31 lines
981 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
// 暴力法获取从1开始到n,有多少个指定的x,[类似于前缀和的思路]
// 从0到10的8次方,就是枚举每一位,一个测试点是 8*10^8,会超时,不可取
int force_count(int n, int x) {
int res = 0;
for (int i = 1; i <= n; i++) {
int t = i;
while (t) {
if (t % 10 == x) res++;
t /= 10;
}
}
return res;
}
int main() {
int a, b;
// 当读入一行为0 0时,表示输入终止,且该行不作处理,注意这里 a||b的使用方法
while (cin >> a >> b, a || b) {
if (a > b) swap(a, b); // 这题还玩小的在后,大的在前,需要我们用代码判断,shit!
// 计算0--9的每一个数出现的次数
for (int i = 0; i <= 9; i++)
cout << force_count(b, i) - force_count(a - 1, i) << ' ';
cout << endl;
}
return 0;
}