Binary Tree POJ - 2499

Background 
Binary trees are a common data structure in computer science. In this problem we will look at an infinite binary tree where the nodes contain a pair of integers. The tree is constructed like this: 

  • The root contains the pair (1, 1). 
  • If a node contains (a, b) then its left child contains (a + b, b) and its right child (a, a + b)


Problem 
Given the contents (a, b) of some node of the binary tree described above, suppose you are walking from the root of the tree to the given node along the shortest possible path. Can you find out how often you have to go to a left child and how often to a right child?

Input

The first line contains the number of scenarios. 
Every scenario consists of a single line containing two integers i and j (1 <= i, j <= 2*10 9) that represent 
a node (i, j). You can assume that this is a valid node in the binary tree described above.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing two numbers l and r separated by a single space, where l is how often you have to go left and r is how often you have to go right when traversing the tree from the root to the node given in the input. Print an empty line after every scenario.

Sample Input

3
42 1
3 4
17 73

Sample Output

Scenario #1:
41 0

Scenario #2:
2 1

Scenario #3:
4 6

题意与思路:

题意就是给你两个数,他们都是由(1,1)通过(a+b,b)转换成右支和(a,a+b)转换成左支,求最少的转换次数

分析:1.根源是(1,1);

            2.左右两点的变化是通过上一层的左右相加得到的结果,所以要想转换最初(1,1)的次数最少;

大的一个数必然要从小的一方叠加而来,这样才可以保证叠加过程变化最少,直到大的减去小的不在大于小的一方;

所以可以得到大的变化次数就是-->大/小,大=大%小,再次进行比较,循环;

若都为1,直接输出,两者变化次数;

若一方为a=1,另一b不为1,由上面所阐述的求值过程,则可以得出最后结果就是在原来变化次数上b所在一方上加(b-1)次

代码如下:

#include<stdio.h>
#include<string.h>
int t,n,m,z=1;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        int r=0,l=0,flag1=0,flag2=0;    //r,l分别代表右边变化次数和左边变化次数,flag1和flag2分别用来标记n,m变化过程中是否值为1.
        while(1)
        {
            if(n==1)
                flag1=1;//如果n=1-->flag1=1;
            if(m==1)
                flag2=1;//如果m=1-->flag2=1;
            if(flag1+flag2==2)
            {
                printf("Scenario #%d:\n%d %d\n\n",z++,l,r);//都为1输出l,r值即可
                break;
            }
            else if(flag1==1&&flag2==0)//如果只存在一个为1,为1的肯定是不用继续改变了,但是不为1的需要在经过另外一个数值-1次的变化
            {
                printf("Scenario #%d:\n%d %d\n\n",z++,l,r+m-1);
                break;
            }
            else if(flag1==0&&flag2==1)
            {
                printf("Scenario #%d:\n%d %d\n\n",z++,l+n-1,r);
                break;
            }
            if(n>m)
            {
                l+=n/m;
                n%=m;
            }
            if(m>n)
            {
                r+=m/n;
                m%=n;
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值