#include <iostream> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(0); int n, numbOfOnes = 0, tmp, onesTmp, nextNumb = 1, delCounter = 0; vector<int> v; cin >> n; int tab[n]; while (numbOfOnes < n) { tmp = nextNumb; v.push_back(nextNumb); onesTmp = 0; while (tmp > 0) { onesTmp += tmp % 2; tmp /= 2; } tab[nextNumb] = onesTmp; numbOfOnes += onesTmp; ++nextNumb; } nextNumb = v.size() - 1; while (numbOfOnes > n) { if (numbOfOnes - tab[v[nextNumb]] >= n) { v[nextNumb] = 0; numbOfOnes -= tab[v[nextNumb]]; delCounter++; } nextNumb--; } cout << v.size() - delCounter << endl; for (int i = v.size() - 1; i >= 0; i--) { if (v[i] != 0) { cout << v[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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <iostream> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(0); int n, numbOfOnes = 0, tmp, onesTmp, nextNumb = 1, delCounter = 0; vector<int> v; cin >> n; int tab[n]; while (numbOfOnes < n) { tmp = nextNumb; v.push_back(nextNumb); onesTmp = 0; while (tmp > 0) { onesTmp += tmp % 2; tmp /= 2; } tab[nextNumb] = onesTmp; numbOfOnes += onesTmp; ++nextNumb; } nextNumb = v.size() - 1; while (numbOfOnes > n) { if (numbOfOnes - tab[v[nextNumb]] >= n) { v[nextNumb] = 0; numbOfOnes -= tab[v[nextNumb]]; delCounter++; } nextNumb--; } cout << v.size() - delCounter << endl; for (int i = v.size() - 1; i >= 0; i--) { if (v[i] != 0) { cout << v[i] << " "; } } } |