文章

1

粉丝

0

获赞

1

访问

134

头像
二叉树2 题解:左右边界法
P1264 北京大学机试题
发布于2026年3月27日 16:58
阅读数 134

#include <stdio.h>

#include <math.h>

#include <string.h>

#include <algorithm>

using namespace std;


 

int main(){

    int m,n;

    while(scanf("%d%d",&m,&n)!=EOF){

        int count=0;

        int left =m; //当前层左边界

        int right = m;//当前层右边界

        while(left<=n){

            int curright = min(right,n);

            count += curright- left +1;

            left = 2* left;//下一层

            right = 2* right+1;

        }

        printf("%d\n",count);

    }

 

}

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发