#include <iostream> int count_bits(int number) { int result = 0; for (; number > 0; result += number & 0b1, number /= 2); return result; } void solution(std::istream& instream, std::ostream& outstream) { int n, start, total_bits = 0; instream >> n; for (start = 0; total_bits < n; start++, total_bits += count_bits(start)); outstream << (total_bits == n ? start : start - 1) << std::endl; for (; count_bits(start) > total_bits - n; start--) outstream << start << " "; for (start--;start>0; start--) outstream << start << " "; } int main() { solution(std::cin, std::cout); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <iostream> int count_bits(int number) { int result = 0; for (; number > 0; result += number & 0b1, number /= 2); return result; } void solution(std::istream& instream, std::ostream& outstream) { int n, start, total_bits = 0; instream >> n; for (start = 0; total_bits < n; start++, total_bits += count_bits(start)); outstream << (total_bits == n ? start : start - 1) << std::endl; for (; count_bits(start) > total_bits - n; start--) outstream << start << " "; for (start--;start>0; start--) outstream << start << " "; } int main() { solution(std::cin, std::cout); } |