-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10binary.cpp
53 lines (45 loc) · 1.19 KB
/
10binary.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
#include <iostream>
#include <string>
#include <bitset>
#include <vector>
#include <algorithm>
int main() {
int decimal;
std::cin >> decimal;
int remainder;
std::vector<int> binary, ones;
int counter {0};
while (decimal > 0) {
remainder = decimal % 2;
decimal /= 2;
binary.insert(binary.begin(), remainder);
if (remainder == 1)
counter++;
else {
ones.insert(ones.begin(), counter);
counter = 0;
}
}
ones.insert(ones.begin(), counter);
std::sort(ones.begin(), ones.end(), std::greater<int>());
std::cout << ones.front() << '\n';
return 0;
}
//// various attempts
/* const int n {20};
std::string binary {std::bitset<n>(decimal).to_string()};
std::string::size_type start {0};
std::string::size_type index {0};
//int count {0};
while (index != std::string::npos) {
index = binary.find('1', start);
std::cout << index << '\n';
if (index != std::string::npos) {
}
} */
/* for (auto c : binary) {
if (c == '1')
count++;
}
std::cout << binary << '\n';
std::cout << count << '\n'; */