题目链接: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. externkeyword放到函数体内而导致的linkage问题

    不少人都知道,C/C++语言编程时,假设要引用在别的C/C++文件里定义的变量或函数,必须extern一下,才干使用 另一些人知道,这个extern事实上能够放在函数体内声明,这么做的理由是不想让其它 ...

  2. js 触摸事件

    js触摸事件 应用在移动端 webkit内核都支持. 触摸事件api https://dvcs.w3.org/hg/webevents/raw-file/tip/touchevents.html 事件 ...

  3. .NET连接SAP系统专题:.NET调用RFC几种方式(一)

    本来今天是要写一篇关于NCO3.0的东西,就是关乎.NET调用SAP的RFC的,支持VS2010和.NET 4.0等.现在网上到处都是充斥着NCO1.X和NCO2.0,需要用VS2003来使用,都是一 ...

  4. Android应用开发提高篇(1)-----获取本地IP

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/21/2361802.html 一.概述 习惯了Linux下的网络编程,在还没用智能机之前就一直想 ...

  5. web应用之监听器

    package com.log.service; import java.util.Enumeration; import javax.servlet.ServletContext; import j ...

  6. Qt多线程编程总结(一)(所有GUI对象都是线程不安全的)

    Qt对线程提供了支持,基本形式有独立于平台的线程类.线程安全方式的事件传递和一个全局Qt库互斥量允许你可以从不同的线程调用Qt方法. 这个文档是提供给那些对多线程编程有丰富的知识和经验的听众的.推荐阅 ...

  7. java csv 文件 操作类

    一个CSV文件操作类,功能比较齐全: package tool; import java.io.BufferedReader; import java.io.BufferedWriter; impor ...

  8. 未能加载文件或程序集“DAL”或其他的某一个依赖项,系统找不到指定的文件

    针对这个问题我在敲VB.NET机房收费系统的时候总共出现了两次,第一次是在使用反射+抽象工厂的时候出现的,第二次是在使用VS2012自带的打包工具生成可执行文件执行exe文件的时候出现的.具体看下图: ...

  9. 一些常用的Intent及intent-filter的信息

    Uri Action 功能 备注 geo:latitude,longitude Intent.ACTION_VIEW 打开地图应用程序并显示指定的经纬度   geo:0,0?q=street+addr ...

  10. sign a third-party dll which don't have a strong name

    Problem: Assembly generation failed -- Referenced assembly '' does not have a strong name Cause: thi ...