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
data
is a string of no more than 10 characters,left_child
andright_child
are 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 ...
随机推荐
- MySQL导入导出数据和表结构 source和mysqldump
MySQL导入数据的方式: 1.使用source /dir/test.sql导入数据进入数据库:查询数据库编码格式show variables like "%char%";设置编码 ...
- python - 小米推送使用
1. 小米文档及SDK下载 1.文档介绍 https://dev.mi.com/console/doc/detail?pId=863 sdk说明: 2.开发者需要登录开发者网站(申请AppID, Ap ...
- 畜禽免疫系统使用LODOP打印
<div class="btn_box"> <asp:Button ID="btnPrint" Text="预览并打印" ...
- 2018 ICPC Asia Singapore Regional A. Largest Triangle (计算几何)
题目链接:Kattis - largesttriangle Description Given \(N\) points on a \(2\)-dimensional space, determine ...
- JDK,JRE,JVM
jdk JDK是整个Java的核心,包括了Java运行环境(Java Runtime Environment),一堆Java工具和Java基础的类库(rt.jar).不论什么Java应用服务器,实质都 ...
- 2.1 Nginx服务器安装
2.1 Nginx目录和文件介绍 windows下解压nginx后的文件介绍: conf:存放Nginx服务器的配置文件,包含Nginx服务器的基本配置文件和对部分特性的配置文件,正确配置此文件可以保 ...
- JavaScript去除数组中重复的数字
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 什么是索引?Mysql目前主要的几种索引类型
一.索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的My ...
- dentityServer4(1)- 特性一览
本地应用只本地客户端应用,例如QQ.微信 IdentityServer4是ASP.NET Core 2的OpenID Connect和OAuth 2.0框架.它可以在您的应用程序中提供以下功能: 它使 ...
- java final关键字详解
final是java中保留关键字,可以声明成员变量.类.方法与本地变量,一旦引用final关键字,将不能再改变这个引用,编译器会检查代码,要是想改变该引用,会报错. final变量? 凡是对成员变量或 ...