您的位置:首页 > 其它

acm-Same binary weight

2013-11-12 16:44 381 查看


Same binary weight

时间限制:300 ms | 内存限制:65535 KB
难度:3

描述

The binary weight of a positive integer is thenumber of 1's in its binary representation.for example,the decmialnumber 1 has a binary weight of 1,and the decimal number 1717(which is 11010110101 in binary) has a binary weight of 7.Give apositive integer
N,return the smallest integer greater than N thathas the same binary weight as N.N will be between 1 and1000000000,inclusive,the result is guaranteed to fit in a signed32-bit interget.

输入
The input has multicases and each case contains a integerN.

输出
For each case,output the smallest integer greater than N that hasthe same binary weight as N.
样例输入
1717
4
7
12
555555


样例输出
1718
8
11
17
555557


来源
topcoder
代码:
#include

#include

#include

#include

using namespace std;

int main()

{

int n;

int i,j,count;

while(scanf("%d",&n)!=EOF)

{

bitset<32> b;

i=0;

while(n)

{

if(n&1)

b.set(i);

i++;

n>>=1;

}

count=0;

for(j=0;j

{

if(b.test(j))

{

count++;

b.reset(j);

if(!b.test(j+1))

{

b.set(j+1);

break;

}

}

}

for(j=0;j

b.set(j);

printf("%d\n",b.to_ulong());

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: