-
Notifications
You must be signed in to change notification settings - Fork 0
/
18_4.cpp
92 lines (83 loc) · 1.59 KB
/
18_4.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// 18_4.cpp
//
//
// Created by Pengyan Qin on 7/23/15.
//
//
#include <iostream>
#include <cmath>
using namespace std;
// brute force: O(n)
int numOf2(int n){
int cnt = 0;
while(n > 0){
if(n%10 == 2){
cnt++;
}
n = n/10;
}
return cnt;
}
int count_2s(int n){
int res = 0;
for(int i = 0; i <= n; ++i){
res += numOf2(i);
}
return res;
}
//count 2s by digit
int count_2ss(int n){
int res = 0;
int digits = 0;
int tmp = n;
while(tmp > 0){
digits++;
tmp /= 10;
}
int dig = 0;
while(dig < digits){
int tens = pow(10, dig);
int low = n%tens;
int high = n/(tens*10);
int val = (n/tens)%10;
if(val < 2){
res += high * tens;
}
else if(val > 2){
res += (high + 1)*tens;
}
else
res += high*tens + low + 1;
dig++;
}
return res;
}
int count_2book(int n){
int count = 0;
int factor = 1;
int low = 0, high = 0, cur = 0;
while(n/factor != 0){
low = n%factor;
high = n/(factor*10);
cur = (n/factor)%10;
if(cur < 2){
count += high * factor;
}
else if(cur > 2){
count += (high + 1)*factor;
}
else
count += high*factor + low + 1;
factor *= 10;
}
return count;
}
int main(){
int n = 62534;
cout << numOf2(n) << endl;
cout << count_2s(n) << endl;
cout << count_2ss(n) << endl;
cout << count_2book(n) << endl;
return 0;
}