zoukankan      html  css  js  c++  java
  • POJ2499Binary Tree思维题

    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 #include<iostream>
     2 #include<algorithm>
     3 #include<string.h>
     4 #include<stdio.h>
     5 #define inf 0x3f3f3f3f
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     std::ios::sync_with_stdio(false);
    11     cin.tie(0);
    12     cout.tie(0);
    13     int n,l,r;
    14     while(cin>>n)
    15     {
    16         int tt=1;
    17         while(n--)
    18         {
    19             cin>>l>>r;
    20             if(l==1)
    21             {
    22                 cout<<"Scenario #"<<tt++<<":"<<endl;
    23                 cout<<0<<" "<<r-1<<endl;
    24                 if(n)
    25                     cout<<endl;
    26                 continue;
    27             }
    28             else if(r==1)
    29             {
    30                 cout<<"Scenario #"<<tt++<<":"<<endl;
    31                 cout<<l-1<<" "<<0<<endl;
    32                 if(n)
    33                     cout<<endl;
    34                 continue;
    35             }
    36             else
    37             {
    38                 int tl=0,tr=0;
    39                 while(1)
    40                 {
    41                     if(l==1)
    42                     {
    43                         tr+=(r-1);
    44                         break;
    45                     }
    46                     else if(r==1)
    47                     {
    48                         tl+=(l-1);
    49                         break;
    50                     }
    51                     else
    52                     {
    53                         if(l>r)
    54                         {
    55 
    56                             tl+=l/r;
    57                             l=l%r;
    58 
    59                         }
    60                         else//l<r
    61                         {
    62                          //   tr++;
    63                             tr+=r/l;
    64                             r=r%l;
    65                             //左边不变
    66                         }
    67                     }
    68                 }
    69                 cout<<"Scenario #"<<tt++<<":"<<endl;
    70                 cout<<tl<<" "<<tr<<endl;;
    71                 if(n)
    72                     cout<<endl;
    73 
    74             }
    75         }
    76     }
    77     return 0;
    78 }
    View Code
    
    
    


  • 相关阅读:
    Spring boot如何快速的配置多个Redis数据源
    SMProxy,让你的数据库操作快三倍!
    利用OpenJ9大幅度降低JAVA内存占用
    利用mitmproxy实现抖音Cookie,设备ID获取(一)
    结合AndServer,实现抖音X-Gorgon算法,设备id生成接口
    抖音APP登录分析,Cookie获取
    Spring Boot 最简单整合Shiro+JWT方式
    shiro使用步骤梳理
    梳理搭建SSM步骤
    SSM中保存数据出现415错误
  • 原文地址:https://www.cnblogs.com/OFSHK/p/11267228.html
Copyright © 2011-2022 走看看