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 ...
随机推荐
- 基于thinkphp的uploadify上传图功能
php Action server端 <?php /* * To change this template, choose Tools | Templates * and open the ...
- jQeury学习笔记
jQuery 语法: 核心语法: $(selector).action() 美元符号定义 jQuery 选择符(selector)"查询"和"查找" HTML ...
- 【转】sqlserver数据库之间的表的复制
以下以数据库t1和test为例. 1.复制表结构及资料 select * into 数据库名.dbo.表名 from 源表(全部数据) 如:select * into t1.dbo.YS1 ...
- comparable与comparator比较
两种比较接口分析 前者应该比较固定,和一个具体类相绑定,而后者比较灵活,它可以被用于各个需要比较功能的类使用. 一个类实现了 Camparable 接口表明这个类的对象之间是可以相互比较的.如果用数学 ...
- 梳理下phpmyadmin改root密码后登录不上的问题。
一, 登陆phpmyadmin,然后点击左侧进入mysql数据库,在顶部点击“mysql”进入sql输入界面.输入以下命令: update user set password=password('12 ...
- PHP将Excel导入数据库以及数据库数据导出至Excel
一.导入 导入需要使用能读取Excel的组件,网上也有比较好的组件,这里分享我使用的:下载 提取码:vxyn.(注意两个文件有引用关系) <?php //传入要导入的Excel的文件名 fun ...
- Proving Equivalences(加多少边使其强联通)
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 【SqlServer数据类型、C#数据类型、SqlDbType】对应关系及转换
// sql server数据类型(如:varchar)// 转换为SqlDbType类型public static SqlDbType SqlTypeString2SqlType(string sq ...
- 利用CSS、JavaScript及Ajax实现图片预加载的三大方法(转)
预加载图片是提高用户体验的一个很好方法.图片预先加载到浏览器中,访问者便可顺利地在你的网站上冲浪,并享受到极快的加载速度.这对图片画廊及图片占据很大比例的网站来说十分有利,它保证了图片快速.无缝地发布 ...
- javaweb学习路之三--websocket多人在线聊天
在之前的项目基础上,加入了一个聊天室的功能,为了界面好看 引入了AmazeUI和umeditor最终效果图如下: 源码在 https://github.com/Zering/MyWeb 目前练习都在这 ...