您的位置:首页 > 其它

Chef Shortest Path in Binary Trees

2014-04-05 09:35 246 查看
如果不在同一层,大的往上;在同一层则同时往上。直到根相同

#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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: