POJ-2499-Binary Tree-思维题
2024-09-05 03:47:20
Background
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:
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
The first line contains the number of scenarios.
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.
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
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing two numbers l and r separated by a single space, where l is how often you have to go left and r is how often you have to go right when traversing the tree from the root to the node given in the input. Print an empty line after every scenario.
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 ;
}
最新文章
- .NET/ASP.NET MVC Controller 控制器(IController控制器的创建过程)
- 深入理解JavaScript中的==运算符
- PlayMaker 学习笔记
- ios开发函数(数学函数应用)
- 从两个平方算法到分治算法-java
- 使用spring rest插入数据库时发生了 前言中不允许有内容 错误
- mysqladmin note
- <;Stackoverflow>; 声望和节制
- Android BLE开发——Android手机与BLE终端通信初识
- String、StringBuffer、StringBuilder比较
- ubuntu wubi.exe 直接加载下载好的 amd64.tar.xz
- 理解java值传递与引用传递
- ajax分页借鉴
- web框架开发-模板层
- Redis高可用方案----Redis主从+Sentinel+Haproxy
- SpringMVC表单验证与Velocity整合
- Idea中lombok不生效原因
- hdu1171 Big Event in HDU(多重背包)
- android升级gradle到3.4.1
- FastReport动态绑定只显示一条数据。
热门文章
- exports和module.exports的区别——学习笔记
- mysql命令行执行时不输出列名(字段名),直接显示字段对应的数值
- Servlet - Servlet相关
- Linkedlist 详解
- Windows API DOC
- Delphi 窗体的释放和判断窗体是否存在
- fedora23上安装和运行MySQL server (MySQL 已经被MariaDB取代)
- LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)
- iOS开发静态库冲突——如何查看静态库(.O)中方法名
- C89,C99: C数组&;结构体&;联合体快速初始化