#include <iostream> #include <cmath> int nearestPowerOfTwo(int n) { int lowerPower = 1; while (lowerPower * 2 <= n) { lowerPower *= 2; } int upperPower = lowerPower * 2; int lowerDistance = std::abs(n - lowerPower); int upperDistance = std::abs(upperPower - n); if (lowerDistance <= upperDistance) { return lowerPower; } return upperPower; } int main() { int n; std::cin >> n; int result = nearestPowerOfTwo(n); std::cout << result << std::endl; return 0; } /************************************************************** Problem: 1075 User: zhouhongyi Language: C++ Result: Accepted Time:9 ms Memory:2072 kb ****************************************************************/