#include <iostream> #include <vector> int ilosc_bitow(int n) { if (n == 0) return 0; int ilosc = 0; while (n > 1) { if (n % 2 == 1) ilosc++; n = n / 2; } ilosc++; return ilosc; } using namespace std; int main() { int n; cin >> n; int suma_bitow = 0; int liczba = 0; vector<int> ciag; while (true) { liczba++; suma_bitow += ilosc_bitow(liczba); if (suma_bitow > n) { suma_bitow -= ilosc_bitow(liczba); break; } ciag.insert(ciag.begin(), liczba); } if (suma_bitow < n) { int tmp = ciag[0]; int poczatkowa = ilosc_bitow(tmp); while ((n - suma_bitow + poczatkowa) != ilosc_bitow(tmp)) { tmp++; } ciag[0] = tmp; } cout << ciag.size() << endl; for (int i = 0; i < ciag.size(); i++) { cout << ciag[i] << ' '; } cout << endl; return 0; }
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 | #include <iostream> #include <vector> int ilosc_bitow(int n) { if (n == 0) return 0; int ilosc = 0; while (n > 1) { if (n % 2 == 1) ilosc++; n = n / 2; } ilosc++; return ilosc; } using namespace std; int main() { int n; cin >> n; int suma_bitow = 0; int liczba = 0; vector<int> ciag; while (true) { liczba++; suma_bitow += ilosc_bitow(liczba); if (suma_bitow > n) { suma_bitow -= ilosc_bitow(liczba); break; } ciag.insert(ciag.begin(), liczba); } if (suma_bitow < n) { int tmp = ciag[0]; int poczatkowa = ilosc_bitow(tmp); while ((n - suma_bitow + poczatkowa) != ilosc_bitow(tmp)) { tmp++; } ciag[0] = tmp; } cout << ciag.size() << endl; for (int i = 0; i < ciag.size(); i++) { cout << ciag[i] << ' '; } cout << endl; return 0; } |