题目链接:http://poj.org/problem?id=2499

思路分析:结点向左边移动时结点(a, b)变为( a+b, b),向右边移动时( a, b )变为( a, a + b); 为求最短路径<a1, a2, a3,...,an>,

考虑从已经知道的结点(a, b)开始找出最短路径回到根节点(1, 1),即向左移动次数和向右移动次数最少回到根节点,由贪心算法,

若 a>b 时,a 减少最大即减去 b,若 a < b,b 减少最大即减去a值,循环直到到达根节点(1, 1)。

代码如下:

#include <iostream>
using namespace std; int main()
{
int n; scanf( "%d", &n );
for ( int i = ; i < n; ++i )
{
int a, b;
int l, r; scanf("%d %d", &a, &b );
l = r = ;
while( a != || b!= )
{
if ( a == )
{
r += b - a;
b = ;
}
else
if ( b == )
{
l += a - ;
a = ;
}
else
if ( a > b )
{
l += a / b;
a -= b * ( a / b );
}
else
if ( a < b )
{
r += b / a;
b -= a *( b / a );
}
} printf( "Scenario #%d:\n%d %d\n\n", i + , l, r );
} return ;
}

Poj 2499 Binary Tree(贪心)的更多相关文章

  1. POJ 2499 Binary Tree

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

  2. POJ 2499 Binary Tree(二叉树,找规律)

    题意:给一个这样的二叉树,每个节点用一对数(a,b)表示,根节点为(1,1).设父亲为(a,b),左儿子(a+b,b),右儿子(a,a+b). 给几组数据,(i,j),求从根节点到(i,j)节点需要向 ...

  3. Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏

    Binary Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6355   Accepted: 2922 Descr ...

  4. 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记

    前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...

  5. 2015上海赛区B Binary Tree

    B - Binary Tree   Description The Old Frog King lives on the root of an infinite tree. According to ...

  6. 863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点

    [抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Retur ...

  7. 536. Construct Binary Tree from String 从括号字符串中构建二叉树

    [抄题]: You need to construct a binary tree from a string consisting of parenthesis and integers. The ...

  8. 742. Closest Leaf in a Binary Tree查找最近的叶子节点

    [抄题]: Given a binary tree where every node has a unique value, and a target key k, find the value of ...

  9. 106. Construct Binary Tree from Inorder and Postorder Traversal根据后中序数组恢复出原来的树

    [抄题]: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assum ...

随机推荐

  1. retina屏实现border边框1px

    .border { position: relative; width: 300px; height: 200px; } .border:after { border: 1px solid #ff33 ...

  2. 使用Notepad++快速有效删除复制代码中的行号

    转载:http://plum.0602.blog.163.com/blog/static/1130006502011101524120757/ 试了该方法,很好用! 为什么我把用Notepad++删除 ...

  3. JavaSE学习总结第27天_反射 & 设计模式 & JDK5、7、8新特性

      27.01  反射_类的加载概述和加载时机 类的加载:当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载:就是指将class文件读 ...

  4. 关于CDH5.2+ 添加hive自定义UDAF函数的方法

  5. PHP的环境搭建

    下载开发环境 wampserver 下载sublime text 2 sublime使用技巧 1:安装漂亮的编程字体http://pan.baidu.com/s/1xMex9 下载"程序编写 ...

  6. 利用python进行数据分析之绘图和可视化

    matplotlib API入门 使用matplotlib的办法最常用的方式是pylab的ipython,pylab模式还会向ipython引入一大堆模块和函数提供一种更接近与matlab的界面,ma ...

  7. 将dll放进exe[.Net]

    原文:将dll放进exe[.Net] 两种方案: 1.使用ILMerge工具. 缺点:需离开工程,使用第三方工具(ILMerge). 2.将dll作为Resource放进exe,exe执行时动态加载( ...

  8. ThinkPHP 3.1.2 模板中的基本语法<1>

    # # ThinkPHP 3.1.2 模板中的基本语法 一.传统的方式,导入CSS和JS文件 1.css link js scr <link rel='stylesheet' type='tex ...

  9. c++ cout 保留小数点位

    需要头文件 <iomanip> 输出时需要用 fixed 和 setprecision() fixed代表输出浮点数,setprecision()设置精度. #include <io ...

  10. Eclipse创建新项目时无法输入项目名的解决方法

    放假耍了那么久,也是该收心忙活了. 今天打开Eclipse新建项目时,发生了一个很奇怪的情况,就是在下面这个位置的输入框无法输入. 经过百度之后,发现解决方案是(原地址点我) Eclipse图标右键 ...