• 暑假集训单切赛第一场 POJ 2309 BST(找规律的题)


    题意:给出一棵二分搜索树,再给一个节点编号n,求以这个节点为根节点的子树叶子节点的最大值与最小值。

    首先求n所在的层数,他的层数就是他的因子中2的个数(规律). n的左右各有num=2^i-1个数。最小值是n-num,最大值是n+num

    #include <iostream>
    #include <algorithm>
    #include <stdio.h>
    #include <string.h>
    #include <queue>
    
    using namespace std;
    int t,n;
    
    int finds(int n){
        for(int i=0;i<=30;i++){
            if((1<<i)<=n && n<(1<<(i+1)))
                return i;
        }
    }
    int main()
    {
        scanf("%d",&t);
        for(int i=1;i<=t;i++){
            scanf("%d",&n);
            int k=0;
            int tmp=n;
            while(1){
                if(tmp%2==0){
                    k++;
                    tmp=tmp/2;
                }
                else{
                    break;
                }
            }
            int mins=n-((1<<k)-1);
            int maxs=n+((1<<k)-1);
            printf("%d %d
    ",mins,maxs);
        }
        return 0;
    }
  • 相关阅读:
    jqurey技术总结
    ie浏览器兼容问题小结
    FIS的合并压缩技术
    对js中数组的一些总结
    浅谈如何面向对象进行封装
    13th week blog
    12th week blog
    11th week blog
    10th week blog
    9th Week blog
  • 原文地址:https://www.cnblogs.com/chenxiwenruo/p/3285561.html
Copyright © 2020-2023  润新知