yonghu 发表于 2015-11-26 08:04:58

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(&quot;%d&quot;,&tt);
while(tt--){
scanf(&quot;%d %d&quot;,&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(&quot;%d\n&quot;,ans);
}
}
页: [1]
查看完整版本: Chef Shortest Path in Binary Trees