现在的位置: 首页 > 综合 > 正文

NYOJ412 Same binary weight 【bitset位操作】

2016年10月04日 ⁄ 综合 ⁄ 共 413字 ⁄ 字号 评论关闭

原题链接

先把从右往左的第一对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 0 232 C/C++ 04-08 10:52:09

抱歉!评论已关闭.