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