Machine programming the binary large collection of questions

Title Description

      1 
      / \
    23
    / \ / \
  4 5 6 7
  / \ / \ / \ / \
as shown above, the positive integer 1, 2, 3, ... constitute an infinite binary tree. From one node to the root (the node number is 1) has a unique path, such as from 5 to root node path (5, 2, 1), the path from the root node to 4 is a (4, 2, 1), the path from the root node to a root node contains only one node 1, so that the path (1). For two nodes x and y, assuming their path to the root node are (x1, x2, ..., 1 ) and (y1, y2, ..., 1 ), then there must exist two positive integers i and j, such that xi and yj from the beginning, there xi = yj, xi + 1 = yj + 1, xi + 2 = yj + 2, ...
the question now is, given x and y, their public requirements parent, ie xi (ie, yj).

Enter a description:

Input data comprises a plurality of sets, each set of data comprising two positive integers x and y (1≤x, y≤2 ^ 31-1).

Output Description:

Each set of data corresponding to output a positive integer xi, i.e. their first common parent.
Example 1

Entry

10 4

Export

2 

algorithm idea:
a functional relationship between the complete binary tree, child nodes of the parent node
code is as follows:
// Write your code here Wallpaper CPP 

// complete binary nature: a child node x, the parent node of x / 2, the comparison determination result obtained 
#include <the iostream> the using namespace STD;
 int main () 
{ int x, Y;
         the while (CIN >> >> X Y) 
        { the while (= X! Y) 
                { / *   IF (X> Y) 
                                X / = 2; 
                        the else { 
                                Y / 2 =; 
                        } optimizer * / 
                        X > Y X = X? / 2 : Y = Y / 2 ; 
                COUT
 
        
                
                       
                }<<x<<endl;
        }
        
        
        return 0;
}
 
     

 


Test Results:

 






Guess you like

Origin www.cnblogs.com/javabai/p/10993029.html