#include <iostream> #include <vector> using namespace std; int DecToBin(int num); int main() { int n; int i = 0; int totalBits = 0; vector<int> seq; cin >> n; while (totalBits < n) { i++; totalBits += DecToBin(i); } while (i > 0) { int bits = DecToBin(i); if (totalBits - bits < n) { seq.push_back(i); } else { totalBits -= bits; } i--; } cout << seq.size() << "\n"; for (int i = 0; i < seq.size(); i++) { cout << seq[i] << " "; } } int DecToBin(int num) { int bits = 0; while (num > 0) { if (num % 2 == 1) { bits++; } num = num / 2; } return bits; }
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 | #include <iostream> #include <vector> using namespace std; int DecToBin(int num); int main() { int n; int i = 0; int totalBits = 0; vector<int> seq; cin >> n; while (totalBits < n) { i++; totalBits += DecToBin(i); } while (i > 0) { int bits = DecToBin(i); if (totalBits - bits < n) { seq.push_back(i); } else { totalBits -= bits; } i--; } cout << seq.size() << "\n"; for (int i = 0; i < seq.size(); i++) { cout << seq[i] << " "; } } int DecToBin(int num) { int bits = 0; while (num > 0) { if (num % 2 == 1) { bits++; } num = num / 2; } return bits; } |