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;
    }



你可能感兴趣的:(NOJ [1373] Endless Tree)