1 条题解
-
0
【例45.1】 高低位交换
解题思路
低16位用n&65535提取并左移16,高16位用n>>16提取,再按位或合并。
正确性说明
计算规则直接来自题目定义。按所述顺序处理时,已完成部分的结果被准确保存在累加量、计数或当前状态中;下一步只加入新增贡献。最终得到完整要求的结果。
易错点
输入可能超过有符号int,使用uint64_t读入与运算。
复杂度
时间O(1),空间O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); uint64_t n; cin >> n; cout << ((n & 65535ULL) << 16 | (n >> 16)) << "\n"; return 0; }
- 1