#include <cstdio> const int MAXB = 32; int main() { int n, s = 0; int ostatnia[MAXB]; for (int i = 0; i < MAXB; ++i) { ostatnia[i] = 0; } scanf("%d", &n); int num = 1, bits, skip = 0; while (s < n) { bits = __builtin_popcount(num); s += bits; ostatnia[bits-1] = num; num += 1; } if (s > n) { skip = ostatnia[s-n-1]; } printf("%d\n", skip == 0 ? num-1: num-2); for (int i = num-1; i > 0; --i) { if (i != skip) printf("%d ", i); } }
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 | #include <cstdio> const int MAXB = 32; int main() { int n, s = 0; int ostatnia[MAXB]; for (int i = 0; i < MAXB; ++i) { ostatnia[i] = 0; } scanf("%d", &n); int num = 1, bits, skip = 0; while (s < n) { bits = __builtin_popcount(num); s += bits; ostatnia[bits-1] = num; num += 1; } if (s > n) { skip = ostatnia[s-n-1]; } printf("%d\n", skip == 0 ? num-1: num-2); for (int i = num-1; i > 0; --i) { if (i != skip) printf("%d ", i); } } |