#include <iostream> using namespace std; int bitPower(int Note); void muz(int N); int main() { int N; cin >> N; muz(N); return 0; } int bitPower(int Note) { int Ret = 0; while (Note) { Ret += Note % 2; Note /= 2; } return Ret; } void muz(int N) { int* Tab = new int[N + 1]; int TabS = 0; int Sum = 0; int i = 0; while (Sum != N) { int iBitPower = bitPower(i + 1); if (Sum + iBitPower <= N) { Sum += iBitPower; Tab[i] = i + 1; TabS++; } else break; i++; } if (Sum != N) for (int j = TabS - 1; j >= 0; j--) { Sum -= bitPower(Tab[j]); Tab[j]++; Sum += bitPower(Tab[j]); if (Sum == N) break; } cout << TabS << "\n"; for (int j = TabS - 1; j >= 0; j--) cout << Tab[j] << " "; cout << "\n"; delete[] Tab; }
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 66 67 68 69 70 | #include <iostream> using namespace std; int bitPower(int Note); void muz(int N); int main() { int N; cin >> N; muz(N); return 0; } int bitPower(int Note) { int Ret = 0; while (Note) { Ret += Note % 2; Note /= 2; } return Ret; } void muz(int N) { int* Tab = new int[N + 1]; int TabS = 0; int Sum = 0; int i = 0; while (Sum != N) { int iBitPower = bitPower(i + 1); if (Sum + iBitPower <= N) { Sum += iBitPower; Tab[i] = i + 1; TabS++; } else break; i++; } if (Sum != N) for (int j = TabS - 1; j >= 0; j--) { Sum -= bitPower(Tab[j]); Tab[j]++; Sum += bitPower(Tab[j]); if (Sum == N) break; } cout << TabS << "\n"; for (int j = TabS - 1; j >= 0; j--) cout << Tab[j] << " "; cout << "\n"; delete[] Tab; } |