POJ-2499-Binary Tree-思维题
Binary trees are a common data structure in computer science. In this problem we will look at an infinite binary tree where the nodes contain a pair of integers. The tree is constructed like this:
- The root contains the pair (1, 1).
- If a node contains (a, b) then its left child contains (a + b, b) and its right child (a, a + b)
Problem
Given the contents (a, b) of some node of the binary tree described above, suppose you are walking from the root of the tree to the given node along the shortest possible path. Can you find out how often you have to go to a left child and how often to a right child?
Input
Every scenario consists of a single line containing two integers i and j (1 <= i, j <= 2*10 9) that represent
a node (i, j). You can assume that this is a valid node in the binary tree described above.
Output
Sample Input
3
42 1
3 4
17 73
Sample Output
Scenario #1:
41 0 Scenario #2:
2 1 Scenario #3:
4 6 看起来像二叉树,实际上是一个不断回溯的数学题
注意一下控制格式就行了
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#define inf 0x3f3f3f3f
using namespace std; int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n,l,r;
while(cin>>n)
{
int tt=;
while(n--)
{
cin>>l>>r;
if(l==)
{
cout<<"Scenario #"<<tt++<<":"<<endl;
cout<<<<" "<<r-<<endl;
if(n)
cout<<endl;
continue;
}
else if(r==)
{
cout<<"Scenario #"<<tt++<<":"<<endl;
cout<<l-<<" "<<<<endl;
if(n)
cout<<endl;
continue;
}
else
{
int tl=,tr=;
while()
{
if(l==)
{
tr+=(r-);
break;
}
else if(r==)
{
tl+=(l-);
break;
}
else
{
if(l>r)
{ tl+=l/r;
l=l%r; }
else//l<r
{
// tr++;
tr+=r/l;
r=r%l;
//左边不变
}
}
}
cout<<"Scenario #"<<tt++<<":"<<endl;
cout<<tl<<" "<<tr<<endl;;
if(n)
cout<<endl; }
}
}
return ;
}
POJ-2499-Binary Tree-思维题的更多相关文章
- Poj 2499 Binary Tree(贪心)
题目链接:http://poj.org/problem?id=2499 思路分析:结点向左边移动时结点(a, b)变为( a+b, b),向右边移动时( a, b )变为( a, a + b); 为求 ...
- 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)节点需要向 ...
- Balanced Binary Tree——经典题
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 2017多校第9场 HDU 6161 Big binary tree 思维,类似字典树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6161 题意: 题目是给一棵完全二叉树,从上到下从左到右给每个节点标号,每个点有权值,初始权值为其标号, ...
- poj 1032 Parliament 【思维题】
题目地址:http://poj.org/problem?id=1032 Parliament Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- LOJ #6669 Nauuo and Binary Tree (交互题、树链剖分)
题目链接 https://loj.ac/problem/6669 题解 Orz yyf太神了,出这种又有意思又有意义的好题造福人类-- 首先\(n\)次询问求出所有节点的深度. 考虑按深度扩展(BFS ...
- CodeForce - 1189 D1. Add on a Tree (思维题)
Note that this is the first problem of the two similar problems. You can hack this problem only if y ...
- poj 2229 一道动态规划思维题
http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...
- 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...
随机推荐
- MapReduce分区数据倾斜
什么是数据倾斜? 数据不可避免的出现离群值,并导致数据倾斜,数据倾斜会显著的拖慢MR的执行速度 常见数据倾斜有以下几类 1.数据频率倾斜 某一个区域的数据量要远远大于其他区域 2.数据大小倾斜 ...
- php 即点即改
html代码 <td><span id="list_order">{$vo.list_order}</span> </td> < ...
- 为什么要使用动态链接库(DLL)
为什么要使用动态链接库(DLL) 第一章 为什么要使用动态链接库(DLL) top 提起DLL您一定不会陌生,在Windows中有着大量的以DLL为后缀的文件,它们是保证Windows正常运行和维 ...
- Delphi流
一.流的概念 流简单说是建立在面向对象基础上的一种抽象的处理数据的工具,它定义了一些处理数据的基本操作,如读取数据,写入数据等,程序员只需掌握对流进行操作,而不用关心流的另一头数据的真正流向.其实 ...
- Delphi GDI(一)
Delphi 7下IGDIPlus库的使用 IGDI+是一个免费开源封装微软GDI+功能的Delphi库,该库使得可以用Delphi语言代码快速简短的实现复杂GDI+应用程序. 官方网站:http:/ ...
- Impala的安装和使用
通过本地yum源进行安装impala 所有cloudera软件下载地址 http://archive.cloudera.com/cdh5/cdh/5/ http://archive.cloudera. ...
- mysql查看数据库大小或者表大小
要想知道每个数据库的大小的话,步骤如下: 1.进入information_schema 数据库(存放了数据库的信息) use information_schema; 2.查询所有数据库的大小: sel ...
- mvn clean package:[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12
原文地址:https://www.cnblogs.com/lxcy/p/8279899.html 事故现场: 解决办法: 一是命令行, mvn clean package -Dmaven.test.s ...
- Python环境出现模块找不到
由于上周脚受伤了,修养了几天没有学习.今天去实验室发现我的编译器跑不动了,出现找不到模块的情况,很奇怪都安装了,也不会提示什么模块找不到. 查找了些资料,发现是因为某个模块的文件损坏或者被覆盖或者安装 ...
- pop&dismiss
//dismiss到根视图 - (void)dismissToRootViewController{ UIViewController *vc = self; while (vc.presenting ...