Advertisement
Robert_JR

1042 - Secret Origins

Oct 7th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <vector>
  7. #define SF scanf
  8. #define PF printf
  9. #define TESTCASE int t;scanf("%d", &t);while(t--)
  10. #define FOR(n) for(int i = 0; i < n; i++)
  11. using namespace std;
  12.  
  13. int set_bit(int n, int position)
  14. {
  15.     return n | (1 << position);
  16. }
  17. int clear_bit(int n, int position)
  18. {
  19.     return n & (~(1 << position));
  20. }
  21. bool get_bit(int n, int position)
  22. {
  23.     return n & (1 << position);
  24. }
  25.  
  26. int main()
  27. {
  28.     int tc = 1;
  29.     int n, temp;
  30.     TESTCASE
  31.     {
  32.         cin >> n;
  33.         bool flag = 0;
  34.         int count = 0;
  35.         FOR(32)
  36.         {
  37.             if(get_bit(n, i))
  38.             {
  39.                 //cout << '~' << get_bit(n, i) << endl;
  40.                 for(int j = i; j < 32; j++)
  41.                 {
  42.                     if(!get_bit(n, j))
  43.                     {
  44.                         // cout << '~' << get_bit(n, j) << endl;
  45.                         flag = 1;
  46.                         n = set_bit(n, j);
  47.                         temp = j-1;
  48.                         break;
  49.                     }
  50.                 }
  51.             }
  52.             if(flag)
  53.             {
  54.                 //cout << flag << endl;
  55.                 for(int k = temp; k >= 0; k--)
  56.                 {
  57.                     if(get_bit(n, k))
  58.                     {
  59.                         //cout << '~' << get_bit(n, k) << endl;
  60.                         count++;
  61.                         n = clear_bit(n, k);
  62.                     }
  63.                     //cout << k << endl;
  64.                 }
  65.             }
  66.             if(flag) break;
  67.         }
  68.         FOR(count-1)
  69.         {
  70.             n = set_bit(n, i);
  71.         }
  72.         PF("Case %d: %d\n", tc++, n);
  73.         //cout << n << endl;
  74.     }
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement