您的位置:首页 > 其它

NYOJ412 Same binary weight 【bitset位操作】

2014-04-08 10:50 281 查看
原题链接

先把从右往左的第一对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
Accepted
0232C/C++04-08 10:52:09
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  NYOJ412