#include <stdio.h> #include <iostream> #include <vector> using namespace std; int howManyOnes(int n){ int cnt = 0; while(n){ if(n&1) cnt++; n >>= 1; } return cnt; } int main(void){ int n, x, cnt = 0, i = 1, diff, outLen; vector<int> tones; cin >> n; while(cnt < n){ x = howManyOnes(i); cnt += x; //printf("%d %d\n", i, x); i++; tones.push_back(x); } //printf("%d\n", cnt - n); diff = cnt - n; i = tones.size() - 1; vector<bool> contained(i+1, true); outLen = i+1; //return 0; while(diff > 0){ if(diff - tones[i] >= 0){ diff -= tones[i]; contained[i] = false; outLen--; //printf("omit tone %d\n", i+1); } i--; } printf("%d\n", outLen); for(int i = tones.size() - 1; i >= 0; i--){ if(contained[i]) printf("%d ", i + 1); } //printf("\n"); 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 | #include <stdio.h> #include <iostream> #include <vector> using namespace std; int howManyOnes(int n){ int cnt = 0; while(n){ if(n&1) cnt++; n >>= 1; } return cnt; } int main(void){ int n, x, cnt = 0, i = 1, diff, outLen; vector<int> tones; cin >> n; while(cnt < n){ x = howManyOnes(i); cnt += x; //printf("%d %d\n", i, x); i++; tones.push_back(x); } //printf("%d\n", cnt - n); diff = cnt - n; i = tones.size() - 1; vector<bool> contained(i+1, true); outLen = i+1; //return 0; while(diff > 0){ if(diff - tones[i] >= 0){ diff -= tones[i]; contained[i] = false; outLen--; //printf("omit tone %d\n", i+1); } i--; } printf("%d\n", outLen); for(int i = tones.size() - 1; i >= 0; i--){ if(contained[i]) printf("%d ", i + 1); } //printf("\n"); return 0; } |