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;
}
相关文章推荐
- 练习场acm 题目412 Same binary weight
- Same binary weight(stl 位运算)
- Same binary weight
- NYOJ 题目412 Same binary weight(bitset)
- nyoj 412 Same binary weight
- Same binary weight bitset(STL)
- NYOJ 412 Same binary weight(bitset位运算)
- Same binary weight
- nyoj-412-Same binary weight(位运算)
- NYOJ 412 Same binary weight(bitset位运算)
- Same binary weight 位操作 bitset用法 STL
- Same binary weight
- 【南理oj】412 - Same binary weight(STL - 双端队列 & 全排列)
- Same binary weight
- Same binary weight
- NYOJ 412 Same binary weight
- NYOJ412 Same binary weight 【bitset位操作】
- Same binary weight 位运算相关
- NYOJ 412 Same binary weight
- Nyist 416 -- Same binary weight