PTA 二叉树路径
二叉树的路径 (25 分)
二叉树是一种普通的数据结构。给出一棵无限的二叉树,节点被标识为一对整数,构造如下:
(1)树根被标识为整数对(1,1)。
(2)如果一个节点被标识为(a,b),那么其左子树树根被标识为(a+b,b),其右子树树根被标识为(a,a+b)。
给出上述二叉树的某个节点标识(a,b),假定从树根到这一给定的节点是沿着最短的路径走,你能给出多少次要向左子树走,多少次要向右子树走?
输入格式:
第一行给出测试用例个数。每个测试用例占一行,由两个整数i和j组成(1<=i, j<=2*109),表示节点的标识(i,j)。假定给出的节点都是有效的节点。
输出格式:
对每个测试用例,第一行为“Scenario #i:”,其中i是测试用例编号,从1开始编号;然后输出一行给出两个整数l和r,中间用1个空格隔开,其中l是从树根到该节点要向左子树走的次数,r是从树根到该节点要向右子树走的次数。在每个测试用例结束后输出一个空行。
输入样例:
3
53 6
7 8
27 95
输出样例:
Scenario #1:
12 1
Scenario #2:
6 1
Scenario #3:
13 4
题意:题目上说了好多,其实就是从(1,1)往下找,找到到(i,j)的最短路径,输出要走多少次左子树,多少次右子树。
一开始我看到最短路径,我第一想到的就是bfs去写,写完之后交了一发,有一组数据内存超限(我是从(1,1)开始往下找)。然后我就改进了一下算法,用逆向思维,反着搜,就没有内存超限了,但是时间超限了(哭笑)......之后,我就仔细的想了一想,找了一个规律,原来这是一个思维题,压根就不是什么搜索题,就是用除法去做,很水...虽然我WA了几次。
#include<iostream>
#include<cstdio>
#include<queue> using namespace std; int main()
{
int n,x,y,p=,l,r;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&x,&y);
l=r=; //l表示要经过左子树的数量,r表示要经过右子树的数量
while(x!=||y!=)
{
if(x>y)
{
if(y==)
l+=x-,x=; //当y为1时,x就只要走x-1步就能变成1了,记住要把x赋为1(跳出的条件所需)
else
l+=x/y,x%=y; //当两个数都不为1时,将其的商加入l中,x的值需要改变成x%y的余数
}
else if(x<y)
{
if(x==)
r+=y-,y=; //同上
else
r+=y/x,y%=x; //同上
}
}
printf("Scenario #%d:\n%d %d\n\n",p++,l,r);
}
return ;
}
PTA 二叉树路径的更多相关文章
- [LeetCode] Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 113. Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [leetcode]257. Binary Tree Paths二叉树路径
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)
Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- LeetCode 112. Path Sum (二叉树路径之和)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- Neo4j WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual
you can add a line in /etc/default/neo4j: NEO4J_ULIMIT_NOFILE=60000 to set the ulimit setting (60000 ...
- 15-Perl 格式化输出
1.Perl 格式化输出Perl 是一个非常强大的文本数据处理语言.Perl 中可以使用 format 来定义一个模板,然后使用 write 按指定模板输出数据.Perl 格式化定义语法格式如下:fo ...
- tracert命令详解_tracert结果详解_tracert命令使用详解
17:06:40 正在等待客服售后工程师令狐冲接入,您可以先简单描述所要咨询的问题,如果长时间没有响应,您也可以 重新选择客服 . 17:06:42 您好,客服售后工程师令狐冲为您服务.售后工程师令狐 ...
- Fonour.AspnetCore 生成SQL SERVER数据库
Install-Package EntityFramework Add-Migration InitialCreate Update-Database
- 解决tomcat端口冲突
1.根据8080端口号查找占用8080端口的进程 netstat -ano|findstr " 进程id:6352 2.根据进程ID查找进程名字 tasklist|findstr " ...
- QT编译Mysql驱动问题及解决方案
默认情况下,qt 并没有自带mysql的数据库插件,需要自己编译先安装mysql server ,运行setup.exe时选择自定义安装,安装目录设为"D:\mysqldev"不要 ...
- linux命令详解——sed
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法 sed命令行格式为: se ...
- busybox date 时间的加减
1.下载安装busybox: # wget http://busybox.net/downloads/busybox-1.29.3.tar.bz2 # tar -jxvf busybox-.tar.b ...
- cpp编码规范要求
1.所有头文件使用#ifndef #define #endif来防止文件被多重包含,命名格式当是: <PROJECT>_<PATH>_<FILE>_H_ 2.只有当 ...
- deep_learning_Function_sklearn.preprocessing.LabelBinarizer()
在多数的机器学习比赛中,给出的标签都是非数字化的,所以我们需要对其进行转换.代码如下: from sklearn import preprocessing feature = [[0,1], [1,1 ...