-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpowers_of_two.cpp
More file actions
65 lines (61 loc) · 1.67 KB
/
powers_of_two.cpp
File metadata and controls
65 lines (61 loc) · 1.67 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
#include <string>
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
/**
* Describe: Describe: Given a positive integer N, it is possible to represent N
* as the sum of several positive or negative powers of 2 (± 2k for some k). For
* example can be represented as 22 + 21 + 20 and 23 + (-20).
* Your task is to find the representation which contains the minimum powers of
* 2.
*/
class Solution {
public:
int minNumberOfPower(int n) {
int len = 1;
int tmp = n;
// Get the length of the binary sequece
while (tmp >>= 1) {
++len;
}
int ones = 0;
int zeros = 0;
int pre_zeros = 0;
int rst = 0;
for (int i = 0; i < len; ++i) {
if (n & (1 << i)) {
// if get one at this position
if (pre_zeros >= 2) {
// number of consecutive zeros greater than 2
tmp = 2 + zeros;
rst += ones < tmp? ones : tmp;
ones = 1;
zeros = 0;
} else {
// number of consecutive zeros not greater than 2
if (ones > 0) {
zeros += pre_zeros;
}
++ones;
}
pre_zeros = 0;
} else {
++pre_zeros;
}
}
tmp = 2 + zeros;
rst += ones < tmp? ones : tmp;
return rst;
}
};
int main() {
Solution so;
int n;
while (cin >> n) {
int len = so.minNumberOfPower(n);
cout << len << endl;
}
return 0;
}