-
Notifications
You must be signed in to change notification settings - Fork 6
/
32_NumberOf1.cpp
60 lines (50 loc) · 1009 Bytes
/
32_NumberOf1.cpp
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
60
#include <iostream>
#include <cstdio>
using namespace std;
unsigned long long NumberOf1Between1AndN(unsigned long long n)
{
//0 : 0
//0~9 : 10 * 0 + 1 = 1
//0~99 : 10 * 1 + 10 = 20
//0~999 : 10 * 20 + 100 = 300
//0~9999 : 10 * 300 + 1000 = 4000
unsigned long long up_to_nine = 0;
unsigned long long weight = 1;
int x;
unsigned long long tmp = n, count = 0;
while (tmp)
{
x = tmp % 10;
count += x * up_to_nine;
if (x > 1)
count += weight;
else if (x == 1)
count += n % weight + 1;
up_to_nine = up_to_nine * 10 + weight;
weight = weight * 10;
tmp = tmp / 10;
}
return count;
}
int main(void)
{
unsigned long long a, b;
int counta = 0, countb = 0;
while (cin >> a >> b)
{
if (a > b)
{
unsigned long long tmp = a;
a = b;
b = tmp;
}
if (a == 0)
counta = 0;
else
counta = NumberOf1Between1AndN(a - 1);
countb = NumberOf1Between1AndN(b);
printf("%d\n", countb - counta);
}
system("pause");
return 0;
}