先把从右往左的第一对01互换位置,再把右边的所有1都移到最右边。
#include <bitset>
#include <iostream>
using namespace std;
int main(){
int n, i, count, j;
while(cin >> n){
bitset<32> bt(n);
for(i = count = 0; i < 32; ++i){
if(bt[i]) ++count;
if(bt[i] && !bt[i+1]){
bt[i] = false;
bt[i+1] = true;
j = i;
break;
}
}
--count;
for(i = 0; i < count; ++i) bt[i] = true;
while(i < j) bt[i++] = false;
cout << bt.to_ulong() << endl;
}
return 0;
}
运行号 | 用户 | 题目 | 结果 | 时间 | 内存 | 语言 | 提交时间 |
---|---|---|---|---|---|---|---|
801892 | 长木 | Same binary weight | ![]() | 0 | 232 | C/C++ | 04-08 10:52:09 |