NOJ [1373] Endless Tree

  • 问题描述
  • There is a endless binary tree, just like the image below. The big boss is in the root, you and your teammate, were transmitted to two nodes -- Nth node and Mth node. In this tree, you can only go up to arrive the root, that means you can't retreat any steps. Now your teammate sent a buff up to root, the buff will go step by step. You want to get that buff to defeat the boss, so you can wait to get this buff, or you can move to catch it, all you should do is to get the buff as quickly as possible, at last, you should tell where you got the buff.


  • 输入
  • For each case, there is two inter N and M, all no more than int.
  • 输出
  • For each case, print number of the node where you got the buff.
    这题不难,只要知道子节点和父节点的关系,father=son/2;
    那么每次就让大的那个数(子节点)除以2;
    最后相等输出就行了
    #include<stdio.h>
    
    int  commonnode(int n,int m)
    {
        while(n!=m)
        {
            if(n<m)
            {
                m>>=1;
            }
            else
            {
                n>>=1;
            }
        }
        return n;
    
    }
    int main()
    {
        int n,m;
        while(~scanf("%d%d",&n,&m))
        {
            int cnt;
            if(n==1 || m==1)
                printf("1\n");
            else
            {
                cnt=commonnode(n,m);
                printf("%d\n",cnt );
            }
            
        }
        return 0;
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值