Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int oneCount(int n)
- {
- int count = 0;
- long long t = 1;
- for(t = 1; t<=n; t*=2)
- {
- if((n&(t))!=0)
- {
- count++;
- }
- }
- return count;
- }
- long long int next(int num)
- {
- long long res,t;
- for(t = 1; t<=num; t*=2)
- {
- if((num&(t))!=0)
- {
- res = num+t;
- break;
- }
- }
- int diff = oneCount(num)-oneCount(res);
- int i;
- for(i = 0; i<diff; i++)
- {
- res+=(1<<i);
- }
- return res;
- }
- int main()
- {
- int tc, k, n;
- scanf("%d", &tc);
- for(k=1; k<=tc; k++)
- {
- scanf("%d", &n);
- printf("Case %d: %d\n", k, next(n));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement