算法基础课 计数问题 二刷打卡
作者:
lazy_go
,
2022-04-17 21:46:29
,
所有人可见
,
阅读 160
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int count(int n, int x){
vector<int> nums;
while(n)nums.push_back(n % 10), n /= 10;
int len = nums.size();
int ans = 0;
for(int i = len - 1 - !x; i >= 0; i --){
int left = 0, right = 0, power10 = 1;
for(int j = len - 1; j > i; j --)left = left * 10 + nums[j];
for(int j = i - 1; j >= 0; j --)right = right * 10 + nums[j], power10 *= 10;
ans += left * power10;
if(!x && left)ans -= power10;
if(nums[i] == x)ans += right + 1;
else if(nums[i] > x)ans += power10;
}
return ans;
}
int main(){
int a, b;
while(cin >> a >> b , a||b){
if(a > b)swap(a, b);
for(int i = 0; i <= 9; i ++){
int ans = count(b, i) - count(a-1, i);
cout << ans << " ";
}
puts("");
}
return 0;
}