#include <iostream> #include <stdio.h> using namespace std; int powerOf(int number) { int count = 0; while (number > 0) { number &= number - 1; count++; } return count; } int main() { int expectedPower; cin >> expectedPower; int lastSound = 0; int totalPower = 0; while (totalPower < expectedPower) { lastSound++; totalPower += powerOf(lastSound); } int surplusPower = totalPower - expectedPower; printf("%d\n", surplusPower == 0 ? lastSound : lastSound - 1); for (int sound = lastSound; sound > 0; sound--) { int power = powerOf(sound); if (power > surplusPower) { printf("%d ", sound); } else { surplusPower -= power; } } 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 | #include <iostream> #include <stdio.h> using namespace std; int powerOf(int number) { int count = 0; while (number > 0) { number &= number - 1; count++; } return count; } int main() { int expectedPower; cin >> expectedPower; int lastSound = 0; int totalPower = 0; while (totalPower < expectedPower) { lastSound++; totalPower += powerOf(lastSound); } int surplusPower = totalPower - expectedPower; printf("%d\n", surplusPower == 0 ? lastSound : lastSound - 1); for (int sound = lastSound; sound > 0; sound--) { int power = powerOf(sound); if (power > surplusPower) { printf("%d ", sound); } else { surplusPower -= power; } } return 0; } |