-
Notifications
You must be signed in to change notification settings - Fork 48
/
1138.c
59 lines (41 loc) · 952 Bytes
/
1138.c
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
@author: Malbolge;
@date: 06/11/2020;
@name: Contagem de Dígitos;
@https://stackoverflow.com/questions/2059680/how-to-count-each-digit-in-a-range-of-integers
*/
#include <stdio.h>
int solve(int, int);
int main(int argc, char **argv)
{
int a, b;
while (scanf("%d %d", &a, &b), a)
{
for (int i = 0; i < 9; ++i)
printf("%d ", solve(b, i) - solve(a - 1, i));
printf("%d\n", solve(b, 9) - solve(a - 1, 9));
}
return 0;
}
int solve(int n, int d)
{
int ans, p, l, pv;
p = 1;
ans = l = pv = 0;
while (n > 0)
{
int x = n % 10;
n /= 10;
ans += x * pv * p / 10;
if (x > d)
ans += p;
else if (x == d)
ans += l + 1;
l += p * x;
p *= 10;
++pv;
}
if (d == 0)
ans -= (p - 1) / 9;
return ans;
}