Descriptionnode
Inputide
First line contains n(1 ≤ n ≤ 10^5), the number of queries. Each query consists of two space separated integers i and j(1 ≤ i, j ≤ 10^9) in one line.ui
Outputspa
Sample Input3d
5 1 2 2 3 4 3 1024 2048 3214567 9998877
Sample Outputcode
1 2 3 1 44
Hintorm
题目意思:这是一个二叉树,如上图所示,给出二叉树上的而已两个点求出一点到另外一点最短的距离或者说是步数。blog
解题思路:咱们知道二叉树至关于一个自上而下的三角形,任意两个点在上方必定有一个点相交,称其为节点,咱们只要找到那个节点就能够了,两点到节点的距离就是最短距离。ip
1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 int t; 6 long long a,b,m,n,count; 7 scanf("%d",&t); 8 while(t--) 9 { 10 scanf("%lld%lld",&a,&b); 11 count=0; 12 while(1) 13 { 14 if(a>b)///两个数较大的那一个减半上移,不断逼近节点 15 { 16 a=a/2; 17 count++; 18 } 19 if(b>a) 20 { 21 b=b/2; 22 count++; 23 } 24 if(b==a) 25 break; 26 } 27 printf("%lld\n",count); 28 29 } 30 return 0; 31 }