PAT A1162 Postfix Expression

news/2025/3/15 17:29:58/

1162 Postfix Expression

分数 25

作者 陈越

单位 浙江大学

Given a syntax tree (binary), you are supposed to output the corresponding postfix 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 and right_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 −1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.

Figure 1Figure 2

Output Specification:

For each case, print in a line the postfix expression, with parentheses reflecting the precedences of the operators.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)%))+)

  每个节点用一个结构体存储,结构体信息应包括结点值,左右孩子编号;
 * 处理步骤先找出根节点,再应根节点进行后序遍历,但应注意:如果有
 * 右孩子而没有左孩子,那么此根节点作为符号前缀,那么应该将根节点
 * 先于孩子结点输出,否则先左孩子再右孩子最后根节点;

/*** 每个节点用一个结构体存储,结构体信息应包括结点值,左右孩子编号;* 处理步骤先找出根节点,再应根节点进行后序遍历,但应注意:如果有* 右孩子而没有左孩子,那么此根节点作为符号前缀,那么应该将根节点* 先于孩子结点输出,否则先左孩子再右孩子最后根节点;
*/#include <iostream>using namespace std;struct Node
{string s;int l, r;
};
const int N = 30;
bool hs[N]; //hs数组为了找出根节点
struct Node tr[N];
int n, root;void Read()
{cin >> n;for(int i=1; i<=n; ++i){string s;int l, r;cin >> s >> l >> r;if(l != -1) hs[l] = 1; if(r != -1) hs[r] = 1;tr[i] = {s, l, r};}
}void beh_traver(int root)
{if(root == -1)  return; //递归边界int l = tr[root].l, r = tr[root].r;string s = tr[root].s; cout << '(';//如果有右孩子而没有左孩子,那么此根节点作为符号前缀,那么应该//将根节点先于孩子结点输出,否则先左孩子再右孩子最后根节点;if(l == -1 && r != -1)  cout << s; beh_traver(l); beh_traver(r);if(l == -1 && r != -1)  cout << ')';else cout << s << ')';
}int main()
{Read();for(root = 1; hs[root] != 0; ++root); //找到根节点beh_traver(root);return 0;
}


http://www.ppmy.cn/news/180984.html

相关文章

软考A计划-电子商务设计师-电子商务系统开发知识

点击跳转专栏>Unity3D特效百例点击跳转专栏>案例项目实战源码点击跳转专栏>游戏脚本-辅助自动化点击跳转专栏>Android控件全解手册点击跳转专栏>Scratch编程案例 &#x1f449;关于作者 专注于Android/Unity和各种游戏开发技巧&#xff0c;以及各种资源分享&am…

【ARMv8 SIMD和浮点指令编程】NEON 加法指令——加法都能玩出花

向量加法包括常见的普通加指令&#xff0c;还包括长加、宽加、半加、饱和加、按对加、按对加并累加、选择高半部分结果加、全部元素加等。如果你和我一开始以为的只有一种普通加&#xff0c;那就太小看设计者了&#xff01;同时这么多加法指令的确会提升我们设计程序的效率&…

惠普580服务器硬盘,HP(惠普)DL580G7服务器配置及详细描述.xlsx

文档介绍&#xff1a; 产品型号序列号描述数量单价 Server HP DL580G7 E7520 2P 16GB Svr 595241 - AA1 标配 2个 Intel 4核 Xeon E7520 处理器(1.86GHz, 18MB 缓存, 95W) , 可扩至四路处理器, Intel7500 芯片组; 集成 iLO3 远程管理, 标配 16GB(4x4GB) DIMMs (DDR3 PC3 - 1060…

centos6.7环境之kvm虚拟化quem工具配置及使用详解

环境准备 需要勾选CPU的虚拟化支持&#xff0c;支持cpu虚拟化的CPU列表&#xff1a; intel支持虚拟化技术CPU列表&#xff1a; Intel 6 Cores / 12 Threads CPU Number&#xff1a;Code Name&#xff1a;Nehalem-EX Xeon E6540 Xeon E7530 Xeon E7540 Xeon L7545Code Name&…

戴尔7050mt支持win7系统

修改bios方法&#xff1a; 1、secure boot找到secure boot enable 设置为disabled。 2、右下角“apply&#xff08;应用&#xff09;”——确认。 3、Ceneral找到Advanced Boot Options 将Enable Legacy Option ROMs 前面的勾打上&#xff1a;点击右下角“apply&#xff08;应用…

在Windows Server 2022系统上安装 Brother MFC-7450打印机驱动

Windows Server 2022系统&#xff0c;接上Brother MFC-7450 打印机&#xff0c;能识别出来&#xff0c;但是提示驱动程序无法使用&#xff08;请忽视打印机图标灰色状态&#xff0c;因为是在打印机关机的状态下截图的。&#xff09; 从打印机官网可以看到驱动最高支持到win10X6…

e系列是服务器CPU吗,Intel-至强E系列CPU参数

《Intel-至强E系列CPU参数》由会员分享,可在线阅读,更多相关《Intel-至强E系列CPU参数(6页珍藏版)》请在人人文库网上搜索。 1、Intel Xeon E系列服务器处理器一、 Intel Xeon E 系列 CPU 命名规则首先, Intel E3,E5,E7 代表了 3 个不同档次的至强CPU,这种命名方式类似桌…

24核48线虚拟化服务器,24核48线程的威力:戴尔PowerEdge R910服务器评测

在“整体设计的提升:初品戴尔PowerEdge R910服务器”一文中,我们通过完整的拆解过程及图片向大家详细介绍了PowerEdge R910这款全功能Intel Xeon 7500平台服务器的硬件结构,4颗英特尔至强E7540 6核CPU(最多支持8核)、24个物理核心和48线程让我们对它的性能充满了期待。本文我…