Light 1042 - Secret Origins (位运算)
## 题意 ##
找出下一个二进制和当前数相同的数。
## 思路 ##
将最低位连续的1的最高位右移一位,其他尽量左移。
## 代码 ##
#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <fstream>
#include <iterator>
#include <iostream>
#include <sstream>
#include <vector>
#include <bitset>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#pragma comment(linker, "/STACK:102400000,102400000")
#include <string>
#include <map>
#include <cmath>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(p, num) memset(p, num, sizeof(p))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)
#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 1000 + 10;
const int MOD = 1000000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int seed = 131;
int cases = 0;
typedef std::pair<int, int> pii;
int Solve(int n)
{
int pos = 0, cnt = 0;
bool look = true;
for (int &i = pos; i < 32; i++)
{
if ((n>>i)&1)
{
look = false;
cnt++;
}
else if (!look) break;
}
int ret = 0;
for (int i = 0; i < pos-1 && cnt-1; i++, cnt--) ret |= (1<<i);
ret |= (1<<pos);
for (int i = pos+1; i < 32; i++) ret |= (((n>>i)&1)<<i);
return ret;
}
int main()
{
//ROP;
int T;
scanf("%d", &T);
while (T--)
{
int n;
scanf("%d", &n);
printf("Case %d: %d\n", ++cases, Solve(n));
}
return 0;
}