求二叉树(123456..)公共父节点

在这里插入图片描述
4和6公共父节点为1
要求多组输入
代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int x,y;	
	while(cin>>x>>y)
	{
    
    
		while(x!=y)
		{
    
    
			if(x>y) x/=2;
			if(x<y) y/=2;
		}
		cout<<x<<endl;		
	}	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43901182/article/details/112999636