阿鑫来了  

        将一棵无穷大满二叉树的结点按根结点一层一层地从左往右编号,根结点编号为1。

现给定a,b为两个结点。设计一个算法,返回a、b最近的公共祖先的编号。

注意其祖先也可能是结点本身。

class LCA {
public:
    int getLCA(int a, int b) {
        while(a!=b){
            if(a>b)
                a=a/2;
            else
                b=b/2;
        }
        return a;
    }
};

 

        求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1

本题含有多组样例输入。

#include<iostream>
using namespace std;

int main(){
    int a;
    
    while(cin>>a){
        int count=0;
        int maxcount=0;
        for(int i=0;i<=256;i++){
            if((a&(1<<i))){
            count++;
            maxcount=max(count,maxcount);
        }else{
            count=0;
        }
    }
    cout<<maxcount<<endl;
    }
    return 0;
}

 

1.与运算(&):  按二进制位进行“与”运算。

规则:0&0=0;   0&1=0;    1&0=0;     1&1=1;       

 

2.或运算(|):  按二进制位进行“或”运算。

 运算规则:0|0=0;   0|1=1;   1|0=1;    1|1=1;

 

3.非运算  (~)    :  取反

运算规则:  ~1 = 0;   ~0 = 1;   ~(10001) = 01110

 

4.异或运算(^):  按二进制位进行“异或”运算。

 运算规则:0^0=0;   0^1=1;   1^0=1;   1^1=0; 

              使用异或交换两个变量的值(不借助第三个变量)

void test(){
    int a = 7;      //7的二进制为0111
    int b = 9;    //9的二进制为1001
    a = a^b;      //1110
    b = a^b;      //0111
    a = a^b;      //1001
    cout << a << ' ' << b << endl;
}

 

posted on 2021-05-13 09:16  阿鑫来了  阅读(119)  评论(0)    收藏  举报