Chef Shortest Path in Binary Trees
如果不在同一层,大的往上;在同一层则同时往上。直到根相同#include<stdio.h>
void swap(int &x,int &y){
x=x^y;
y=x^y;
x=x^y;
}
int main(){
int tt,x,y,z;
scanf("%d",&tt);
while(tt--){
scanf("%d %d",&x,&y);
if(x>y) swap(x,y);
int ans=0;
while(x!=y){
if(y>=x*2){
y/=2;ans++;
}
else{
x/=2,y/=2;
ans+=2;
}
}
printf("%d\n",ans);
}
}
页:
[1]