-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem016.cpp
41 lines (39 loc) · 1017 Bytes
/
problem016.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
#include <cmath>
#include <iostream>
#include <vector>
int sumOfDecimalDigits(const std::vector<bool>& bits)
{
// Double dabble algorithm for converting binary to binary-coded decimal.
std::vector<char> s(ceil(bits.size() / 3.0));
int pos = s.size() - 1;
for (int i = 0; i < bits.size(); ++i) {
for (int j = pos; j < s.size(); ++j) {
if (s[j] >= 5) {
s[j] += 3;
}
}
if ((s[pos] & 0x8) != 0) {
--pos;
}
for (int j = pos; j < s.size() - 1; ++j) {
s[j] <<= 1;
s[j] &= 0xf;
s[j] |= ((s[j + 1] & 0x8) != 0);
}
s[s.size() - 1] <<= 1;
s[s.size() - 1] &= 0xf;
s[s.size() - 1] |= bits[i];
}
int sum = 0;
for (int i = pos; i < s.size(); ++i) {
sum += s[i];
}
return sum;
}
int main()
{
std::vector<bool> bits(1001);
bits[0] = true;
std::cout << "Answer: " << sumOfDecimalDigits(bits) << '\n';
return 0;
}