BST POJ - 2309

版权声明:菜鸟一枚~~ 有想法可在下面评论, 转载标明出处即可。 https://blog.csdn.net/KLFTESPACE/article/details/84866607
#include<iostream>
using namespace std;

typedef long long ll;
/*
ll lowbit(int x)
{
    return x&(x^(x-1));
}
*/


/*
 * 取出x的最低位1 <=> 把k的二进制的高位1全部清空,只留下最低位的1求2^p。
 * (p 为 x 的二进制表示数中, 右向左数第一个1的位置,从第0个位置开始数,也可以是0的个数),如8的二进制表示为1010,向左到右有1个0,第1个二进制位为1,则p=1,故Lowbit(10) = 2^1 = 2。

 */
ll lowbit(ll x)
{
    return x&(-x);//按位与运算
}

int main()
{
    int t;
    ll n, k;

    cin >> t;

    while(t--)
    {
        cin >> n;

        k=lowbit(n);

        cout << n-k+1 << " " << n+k-1 << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/KLFTESPACE/article/details/84866607
BST