Poj 2499 Binary Tree(贪心)
题目链接: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(贪心)的更多相关文章
- POJ 2499 Binary Tree
题意:二叉树的根节点为(1,1),对每个结点(a,b)其左结点为 (a + b, b) ,其右结点为 (a, a + b),已知某结点坐标,求根节点到该节点向左和向右走的次数. 分析:往回一步一步走肯 ...
- POJ 2499 Binary Tree(二叉树,找规律)
题意:给一个这样的二叉树,每个节点用一对数(a,b)表示,根节点为(1,1).设父亲为(a,b),左儿子(a+b,b),右儿子(a,a+b). 给几组数据,(i,j),求从根节点到(i,j)节点需要向 ...
- Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏
Binary Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6355 Accepted: 2922 Descr ...
- 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...
- 2015上海赛区B Binary Tree
B - Binary Tree Description The Old Frog King lives on the root of an infinite tree. According to ...
- 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 ...
- 536. Construct Binary Tree from String 从括号字符串中构建二叉树
[抄题]: You need to construct a binary tree from a string consisting of parenthesis and integers. The ...
- 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 ...
- 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 ...
随机推荐
- zoj 3708 Density of Power Network
/*看英文和图我头都大了,不过很简单的.*/ #include<string.h> #include<stdio.h> ][],q[],w[]; int main(int ar ...
- 【转】CoreData以及MagicalRecord (二)
3. 运行时类与对象 NSManagedObject Managed Object 表示数据文件中的一条记录,每一个Managed Object在内存中对应的实体(Entity)的一个数据表示.Man ...
- Jsp指令有那些?
<%@page language="java" contentType="text/html;charset=gb2312" session=" ...
- BZOJ 3224: Tyvj 1728 普通平衡树(BST)
treap,算是模板题了...我中间还一次交错题... -------------------------------------------------------------------- #in ...
- 操作百度API
string json = ""; try { //虽然两者都是异步请求事件,但是WebClient是基于事件的异步,而HttpWebRequst是基于代理的异步编程 WebCli ...
- Lucence.net索引技术 二
一. Lucene索引创建和优化 [版本2.9.0以上] Lucene索引的创建首先需要取得几个必须的对象: 1.分词器//可以采用其他的中文分词器 StandardAnalyzer analyzer ...
- java 简单的词法分析
package com.seakt.example; import java.io.*; import java.lang.String; public class J_Scanner { publi ...
- C和指针 读书笔记
准备复习一下之前读过的<C和指针>,主要看之前标记过的地方. 感觉像第一次看的地方再记录一下-- 1.预处理器读入源代码,根据预处理指令对其进行修改,然后将修改后的源代码交给编译器. 2. ...
- Win32 SecuritySetting
http://flylib.com/books/en/2.21.1.207/1/ http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/ ...
- web应用中Spring ApplicationContext的动态更新
在web应用中时常需要修改配置,并动态的重新加载ApplicationContext.比如,设置和切换数据库.以下给出一个方法,并通过代码验证可行性. 方法的基本思路是,为WebApplication ...