UVA 1640 The Counting Problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cmath>
using namespace std;
int a, b;
int cnt(int n) {
int res = 0;
while (n) ++res, n /= 10;
return res;
}
int cnt(int n, int x) {
int res = 0, d = cnt(n);
for (int i = 1; i <= d; ++i) {
int p = pow(10, i - 1), l = n / p / 10, r = n % p, di = n / p % 10;
if (x) res += l * p;
else if (l) res += (l - 1) * p;
if (di > x && (x || l)) res += p;
else if (di == x && (x || l)) res += r + 1;
}
return res;
}
int main() {
while (scanf("%d%d", &a, &b), a) {
if (a > b) swap(a, b);
for (int i = 0; i <= 9; ++i)
printf("%d%c", cnt(b, i) - cnt(a - 1, i), i == 9 ? '\n' : ' ');
}
return 0;
}