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 ...
随机推荐
- ajax 工作原理
Ajax的优缺点及工作原理? 定义和用法: AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML).Ajax 是一种用于创建快速动态网 ...
- Stack&Heap的理解
Heap(堆):在英文中有杂乱的堆意思,意译中文为堆:其特点为先进先出. 堆空间分配:一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收,分配方式倒是类似于链表. Stack(栈):在英 ...
- Webstorm 、ECMAScript 6 、AngularJS
突然接到一智障询问以上内容,大发善心总结(其实就是CV大法)一下下,希望能帮助某智障.....上干活! WebStorm 刚看到我是懵逼的,但是看到和其他两个在一起,猜想到是前台的一开始以为是前台框架 ...
- ps去除元素的三种常用方法
1.仿制图章工具,alt+鼠标左键进行选取复制区域,然后左键点击需要覆盖的区域. 2.套锁工具--选择区域--右键填充--内容识别. 3.修补工具,选中区域--拖动适配. 附带另一份较 ...
- leetcood学习笔记-70-爬楼梯
题目描述: 第一次提交:(超时) class Solution: def climbStairs(self, n: int) -> int: if n == 0 or n == 1 or n = ...
- 43 编译原理及cmake使用手册学习
0 引言 大量开源库需要通过cmake编译后使用,了解cmake的基本指令以及CMakeLists.txt的写法非常重要,其基础是了解编译原理.另外,为了对cmake编译的代码进行调试,需要了解CMa ...
- SSDT
2.系统服务调度表SSDT及SSSDT Shadow 系统服务:由操作系统提供的一组函数(内核函数),API可以间接或者直接的调用系统服务.操作系统以动态链接库(DLL)的形式提供API. SSDT: ...
- [zz]winform 窗体关闭事件
注册窗体关闭事件: 在Form1.Designer.cs 文件中添加: this.FormClosing += new System.Windows.Forms.FormClosingEventHan ...
- idea中选中了一个变量名,会高亮显示位于别的地方的这个变量名,那么怎么修改其他地方的高亮颜色
- pip安装时ReadTimeoutError解决办法
主要是被阻断了,所以可以延长等待时间完美解决问题. 在下载终端先输入如下命令: pip --default-timeout=100 install -U pip 然后输入下载命令:pip insta ...