-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem17.cpp
More file actions
77 lines (68 loc) · 1.36 KB
/
problem17.cpp
File metadata and controls
77 lines (68 loc) · 1.36 KB
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
#include <iostream>
#include <unordered_map>
std::unordered_map<int, int> subTwenty =
{
{ 0, 4 },
{ 1, 3 },
{ 2, 3 },
{ 3, 5 },
{ 4, 4 },
{ 5, 4 },
{ 6, 3 },
{ 7, 5 },
{ 8, 5 },
{ 9, 4 },
{ 10, 3 },
{ 11, 6 },
{ 12, 6 },
{ 13, 8 },
{ 14, 8 },
{ 15, 7 },
{ 16, 7 },
{ 17, 9 },
{ 18, 8 },
{ 19, 8 }
};
std::unordered_map<int, int> tens =
{
{ 2, 6 },
{ 3, 6 },
{ 4, 5 },
{ 5, 5 },
{ 6, 5 },
{ 7, 7 },
{ 8, 6 },
{ 9, 6 }
};
int main()
{
unsigned long result = 11; // "one thousand" --> 11
// starts from 1
const auto digitSum = [] () -> unsigned long
{
unsigned long ret = 0;
for (int iDigits = 1; iDigits < 10; ++iDigits)
ret += subTwenty[iDigits];
return ret;
}();
const auto subTwentySum = [] () -> unsigned long
{
unsigned long ret = 0;
for (int iSubTwenty = 1; iSubTwenty < 20; ++iSubTwenty)
ret += subTwenty[iSubTwenty];
return ret;
}();
// Less than one hundred starting from .
result += subTwentySum;
for (int iTens = 2; iTens < 10; ++iTens)
result += tens[iTens] * 10 + digitSum;
// From 100 to 999.
result += (900 - 9) * 10 + 9 * 7; // 891x "hundred and" + 9x "hundred"
for (int iHundreds = 1; iHundreds < 10; ++iHundreds) {
result += subTwenty[iHundreds] * 100;
result += subTwentySum;
for (int iTens = 2; iTens < 10; ++iTens)
result += tens[iTens] * 10 + digitSum;
}
std::cout << result << std::endl;
}