Description
Our problem is to create a function: uint NextKBitNumber(uint),
which given an unsigned integer X with K bits set, returns the
immediately larger unsigned integer also with K bits set. For
example, if X = 12 (base 10) = 1100(base 2), X has 2 bits set,
then the next number with 2 bits
set(NextKBitNumber) is 17(base 10) = 10001(base 2).
Input
The first line is the number of test cases,there are at most 1001
cases.
Then each line is a case with an unsigned number
X.
It is guaranteed that all answers are in unsigned 32-bit
Integer.
Output
For each cases, the first line is “Case #k:”, where k is the case
index based 1.
The second line is the NextKBitNumber of X.
Sample Input
2
12
7
Sample Output
Case #1:
17
Case #2:
11
#include<iostream>
using namespace std;
int main()
{
long long
x,n,b,t,c,m,r,i;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>x;
b=x&-x;
t=x+b;
c=x^t;
m=(c>>2)/b;
r=t|m;
cout<<"Case
#"<<i<<":"<<endl;
cout<<r<<endl;
}
}
本文介绍了一个算法问题:如何找到比给定整数X更大的下一个整数,且该整数同样具有K位1。文章提供了输入输出样例及C++实现代码。

被折叠的 条评论
为什么被折叠?



