题意:给一个这样的二叉树,每个节点用一对数(a,b)表示,根节点为(1,1)。设父亲为(a,b),左儿子(a+b,b),右儿子(a,a+b)。
  给几组数据,(i,j),求从根节点到(i,j)节点需要向左子树走多少次,往右子树多少次。
思路:可以发现:当i>j时,(i,j)为左儿子;当i<j时,(i,j)为右儿子。
    设父节点为(a,b),往左子树需要走l次,往右子树需要走r次,那么:
    1.i>j时:i=a+b,j=b -> a=i-j,b=j l++;
    2.i<j时:i=a,j=a+b -> a=i,b=j-i r++;

当然,写代码时不能一个一个减,会超时的。一次性把能减的都减去。

#include <iostream>
#include <stdio.h>
/*
AC
给一个这样的二叉树,每个节点用一对数(a,b)表示,根节点为(1,1)。设父亲为(a,b),左儿子(a+b,b),右儿子(a,a+b)。
给几组数据,(i,j),求从根节点到(i,j)节点需要向左子树走多少次,往右子树多少次。
思路:可以发现:当i>j时,(i,j)为左儿子;当i<j时,(i,j)为右儿子。
设父节点为(a,b),往左子树需要走l次,往右子树需要走r次,那么:
1.i>j时:i=a+b,j=b -> a=i-j,b=j l++;
2.i<j时:i=a,j=a+b -> a=i,b=j-i r++; */
using namespace std; int main()
{
int t;
scanf("%d",&t);
long long i,j;
int l,r;
for(int w=;w<=t;w++){
l=;r=;
scanf("%lld%lld",&i,&j);
while(){
if(i== && j==)
break;
/*
若i>j,只要求i中有多少个j,即i/j,l+=i/j,最后再把i剩下的赋值给i
若i<j,只要求j中有多少个i,即j/i,r+=j/i,最后再把j剩下的赋值给j
*/
if(i>j){
l+=i/j;
i-=i/j*j;
//若i是j的倍数,即当j=1时,l要减去1,退出循环
//也可以上面l+=(i-1)/j,这样就不用讨论i=0的情况了。
if(i==){
l--;
break;
}
}
else{
r+=j/i;
j-=j/i*i;
//若j是i的倍数,即当i=1时,r要减去1,退出循环
//也可以上面r+=(j-1)/i,这样就不用讨论j=0的情况了。
if(j==){
r--;
break;
}
}
}
printf("Scenario #%d:\n",w);
printf("%d %d\n",l,r);
puts("");
}
return ;
}

POJ 2499 Binary Tree(二叉树,找规律)的更多相关文章

  1. [HDOJ5573]Binary Tree(找规律,贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5573 这个题……规律暂时还找不到,先贡献两发TLE的代码吧,一个dfs一个状压枚举. #include ...

  2. Poj 2499 Binary Tree(贪心)

    题目链接:http://poj.org/problem?id=2499 思路分析:结点向左边移动时结点(a, b)变为( a+b, b),向右边移动时( a, b )变为( a, a + b); 为求 ...

  3. POJ 2499 Binary Tree

    题意:二叉树的根节点为(1,1),对每个结点(a,b)其左结点为 (a + b, b) ,其右结点为 (a, a + b),已知某结点坐标,求根节点到该节点向左和向右走的次数. 分析:往回一步一步走肯 ...

  4. HDU 5573 Binary Tree(找规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5573 题意:给你一个完全二叉树,节点为自然数的排列(第一行1,第二行2 3,第三行4 5 6 7... ...

  5. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  6. [LeetCode] 543. Diameter of Binary Tree 二叉树的直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  7. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  8. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  9. Full Binary Tree(二叉树找规律)

    Description In computer science, a binary tree is a tree data structure in which each node has at mo ...

随机推荐

  1. brewhome - 第三方包管理工具

    最近对移动开发感兴趣,于是乎有了相当正式的理由购买了一台macbook pro 13. 我虽然以前没有使用过mac os,但是上手却很快,这大概跟我最近几年一直在使用linux系统有关吧.我平时上班时 ...

  2. jeecms获取栏目标题图

    [@cms_channel id='1'] <img src="${tag_bean.titleImg!site.typeImg}" /> [/@cms_channel ...

  3. RouterOS的MikroTik脚本从DNS更新IPSEC端的IP地址

    #Script for changing IPSEC address when DNS changes. #Script will iterate through all peers looking ...

  4. Messages.pas里的消息

    一.Windows 消息大全 这张表拷贝自万一兄的帖子:http://www.cnblogs.com/del/archive/2008/02/25/1079970.html 但是我希望自己能把这些消息 ...

  5. Sql Server数据库

    1.表分区 http://www.cnblogs.com/huangxincheng/p/3565755.html 2.MVP教程地址:http://www.cnblogs.com/lyhabc/p/ ...

  6. How to use the SQLIOSim utility to simulate SQL Server activity on a disk subsystem

    SQLIOSim是模拟SQLServer的行为来测试IO性能,也可以对损坏磁盘进行一定校验 这是一个SQL Server 2012 安装完后自带的工具 一般在C:\Program Files\Micr ...

  7. PDF打印

    问题: Microsoft Excel 不能访问文件“D:\bwms\源代码\Dcjet.BWMS\Dcjet.Bwms.Web\PrintTmp\Check_bwms.xls”. 可能的原因有以下几 ...

  8. hg211g破解获取管理员密码,可以连接路由器。默认光猫来拨号。

    先通过这种方式获取telecomadmin密码:1.使用useradmin用户登录设备2.在IE地址栏输入该路径”192.168.1.1/backupsettings.html”3.点击保存设备备份配 ...

  9. 关于Resources.LoadAssetAtPath

    返回的是Object, 返回所在资源路径上的一个资源, 只能应用在unity的编辑器模式下,安卓等平台无效 static function LoadAssetAtPath (assetPath : s ...

  10. 【原创】书本翻页效果booklet jquery插件系列之简介

    booklet jquery插件系列之简介 本文由五月雨恋提供,转载请注明出处. 一.安装 1.添加CSS和Javascript 添加booklet CSS文件到你的页面. <link rel= ...