-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
35 lines (30 loc) · 781 Bytes
/
main.cpp
File metadata and controls
35 lines (30 loc) · 781 Bytes
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
#include <iostream>
#include <vector>
#include <cmath>
int count2helper(int number, int d) {
int pow10 = pow(10, d);
int nextPow10 = pow10 * 10;
int right = number % pow10;
int roundDown = number - number % nextPow10;
int roundUp = roundDown + nextPow10;
int digit = (number / pow10) % 10;
if (digit < 2) {
return roundDown / 10;
} else if (digit == 2) {
return roundDown / 10 + right + 1;
} else {
return roundUp / 10;
}
}
int count2(int number) {
int count = 0;
int len = std::to_string(number).size();
for (int digit = 0; digit < len; digit++) {
count += count2helper(number, digit);
}
return count;
}
int main() {
std::cout << count2(23) << std::endl;
return 0;
}