PAT_A1130#Infix Expression
Source:
Description:
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with parentheses reflecting the precedences of the operators.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the i-th line corresponds to the i-th node) in the format:
data left_child right_child
where
datais a string of no more than 10 characters,left_childandright_childare the indices of this node's left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by −. The figures 1 and 2 correspond to the samples 1 and 2, respectively.
Figure 1 Figure 2
Output Specification:
For each case, print in a line the infix expression, with parentheses reflecting the precedences of the operators. Note that there must be no extra parentheses for the final expression, as is shown by the samples. There must be no space between any symbols.
Sample Input 1:
8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1
Sample Output 1:
(a+b)*(c*(-d))
Sample Input 2:
8
2.35 -1 -1
* 6 1
- -1 4
% 7 8
+ 2 3
a -1 -1
str -1 -1
871 -1 -1
Sample Output 2:
(a*2.35)+(-(str%871))
Keys:
Code:
/*
Data: 2019-08-11 21:33:00
Problem: PAT_A1130#Infix Expression
AC: 16:51 题目大意:
打印中缀表达式
输入:
第一行给出,结点个数N<=20
接下来N行,给出结点i(1~N)的,键值,左孩子编号,右孩子编号(空子树-1) 基本思路:
静态树存储,输出中缀表达式
*/
#include<cstdio>
const int M=1e2;
struct node
{
char data[];
int l,r;
}infix[M]; void Express(int root, int rt)
{
if(root == -)
return;
if(root!=rt && (infix[root].l!=- || infix[root].r!=-))
printf("(");
Express(infix[root].l,rt);
printf("%s", infix[root].data);
Express(infix[root].r,rt);
if(root!=rt && (infix[root].l!=- || infix[root].r!=-))
printf(")");
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,h[M]={};
scanf("%d\n", &n);
for(int i=; i<=n; i++)
{
scanf("%s %d %d\n", infix[i].data, &infix[i].l, &infix[i].r);
if(infix[i].l!=-) h[infix[i].l]=;
if(infix[i].r!=-) h[infix[i].r]=;
}
for(int i=; i<=n; i++)
if(h[i]==)
Express(i,i); return ;
}
PAT_A1130#Infix Expression的更多相关文章
- PAT1130:Infix Expression
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- A1130. Infix Expression
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT A1130 Infix Expression (25 分)——中序遍历
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT 甲级 1130 Infix Expression
https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- PAT 1130 Infix Expression[难][dfs]
1130 Infix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspond ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- PAT 1130 Infix Expression
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- 1130. Infix Expression (25)
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
随机推荐
- Java关于Math类的三个取整方法
0x01 在java的Math类中有三个关于浮点数取整数的方法,分别是ceil (向上取整) floor(向下取整) round(四舍五入) 三个方法 0x02 ceil 向上取整,取整后总是比原来的 ...
- 数据分析系列篇:玩转excel
数据分析系列篇:玩转excel 不知道现在怎么也变得这么鸡婆,连excel都要准备写一篇.没办法,还有很多不是做数据的小伙伴们不会excel啊,抱着不抛弃.不放弃的态度,就讲下excel如何玩转.其实 ...
- MVC路由解析---UrlRoutingModule
文章引导 MVC路由解析---IgnoreRoute MVC路由解析---MapRoute MVC路由解析---UrlRoutingModule Area的使用 引言: 此文全文内容90%转自 一.前 ...
- Mosaic
Mosaic 是最古老的WEB浏览器,这是个套件FOR WINDOWS(包括TCP/IP,拨号,EMAIL等).
- EOJ 1058. 挤模具 (多边形面积)
题目链接:1058. 挤模具 题意 给出模具的底和体积,求模具的高. 思路 模具的底为多边形,因此求出多边形面积,用体积除以底的面积就是答案. 多边形的面积求解见 EOJ 1127. 多边形面积(计算 ...
- TreeMap源码解析笔记
常见的数据结构有数组.链表,还有一种结构也很常见,那就是树.前面介绍的集合类有基于数组的ArrayList,有基于链表的LinkedList,还有链表和数组结合的HashMap,今天介绍基于树的Tre ...
- Zookeeper-技术专区-运作流程分析介绍
Zookeeper的启动流程 Zookeeper的主类是QuorumPeerMain,启动时读取zoo.cfg配置文件,如果没有配置server列表,则单机模式启动,否则按集群模式启动,这里只分析集群 ...
- react 16.3+ 新生命周期
react 16.3版本出现了两个新的生命周期函数,并将逐渐废弃componentWillMount().componentWillReceiveProps().componentWillUpdate ...
- rabbitmq AmqpClient 使用Topic 交换机投递与接收消息,C++代码示例
// strUri = "amqp://guest:guest@192.168.30.11:8820/test" // strUri = "amqp://[帐户名]:[密 ...
- 关于计算机学习的书(doc,mobi,epub,pdf四种格式)
关于计算机学习的书(doc,mobi,epub,pdf四种格式) <html> <body> <div> 21天学通C+ +2016/6/22 18:47文條 30 ...