18、<简单>寻找距离2的幂最近的数字

📅 2026/7/5 13:33:21
18、<简单>寻找距离2的幂最近的数字
#include iostream using namespace std; int main() { int n; cout 请输入整数n; cin n; // 先找到小于等于n的最大2的幂 low int low 1; while (low * 2 n) { low * 2; } int high low * 2; // 大于n的最小2的幂 int dis_low n - low; int dis_high high - n; // 距离相等选小的距离不等选近的 if (dis_low dis_high) cout 距离最近的2的幂 low endl; else if (dis_high dis_low) cout 距离最近的2的幂 high endl; else cout 距离最近的2的幂 low endl; return 0; }