首页 > 专题系列 > Java解POJ > POJ 2309 BST [解题报告] Java
2013
11-11

POJ 2309 BST [解题报告] Java

BST

问题描述 :

Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, …. In a subtree whose root node is X, we can get the minimum number in this subtree by repeating going down the left node until the last level, and we can also find the maximum number by going down the right node. Now you are given some queries as “What are the minimum and maximum numbers in the subtree whose root node is X?” Please try to find answers for there queries.

输入:

In the input, the first line contains an integer N, which represents the number of queries. In the next N lines, each contains a number representing a subtree with root number X (1 <= X <= 231 – 1).

输出:

There are N lines in total, the i-th of which contains the answer for the i-th query.

样例输入:

2
8
10

样例输出:

1 15
9 11

解题代码:

//* @author  [email protected]
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        long n=scan.nextInt();
        long k=0,j,v,temp,l,r;
        for(int i=0;i< n;i++){
            k=scan.nextInt();
            if(k%2==1)System.out.println(k+" "+k);
            else{
                j=(int)Math.floor(Math.log(k)/Math.log(2));
                v=1<< j;
                while(v!=k){
                    j--;
                    if(v>k)v-=(1<< j);
                    else v+=(1<< j);
                }
                l=k;
                r=k;
                l-=(1<< j)-1;
                r+=(1<< j)-1;
                System.out.println(l+" "+r);
            }
        }
    }
}

  1. 站长好。我是一个准备创业的互联网小白,我们打算做一个有关国*际*游*学的平台。手上也有了一些境外资源。现阶段的团队现在没有cto.原意出让一些管理股寻找一个靠谱的技术专家做合伙人, 不知道是不是能得到您的帮助。发个帖子或者其他方式。期待您的回应。可以加我微信tianxielemon聊聊。

  2. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?

  3. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。