Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1562292
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2017-06-19 09:05:58

A formula consisting of four fundamental arithmetic operations c
an be expressed in a binary tree. Following diagram depicts a fo
rmula “(9/(6-4))*3” in a binary tree. If an operator is in a pea
k, the operator is applied using the result of the left subtree
and result of right subtree.




Create a program outputting the calculation result of a formula
when a binary tree consisting of only the operators “+,-, *, /”
and positive integer is given.  All operations are based on real
number operations. If the final result is not an integer, only t
he integer part is output. In the above, the calculation result
is 13.5, thus 13 is output.
 
 
[Constraints]
Total number of peaks N is 1≤N≤1000.
 
 
[Input]
In the first line of the input file, total number of peaks N(1≤N
≤1000) of the tree of each test case is given. The peak data are
given in the next n lines. If the peak is a number, the peak num
ber and the value are given. If the peak is an operator, the pea
k number, operator, peak number of left child and peak number of
right child are given in that order.

An integer between 1 ~ N is given as the peak number. There is n
o special rule for numbering the peak.However, the number of the
root peak is always 1.

The adjacent numbers or operators are all separated by the space.
In the above example, if the number 4 corresponds to the 7th pea
k, it is given “7 4”. If the operator ‘/’ corresponds to the 2nd
peak, it is given “2 / 4 5” since two child nodes are the 4th pe
ak with number 9 and 5th peak with operator ‘-‘.
Total of 10 test cases are given.

[Output]
Output the answer to each test case. Each line begins with ‘#x’,
and the answer is recorded after a line feed. Note that the answ
er is always recorded in an integer form.

[Input Example]

5 TestCase 1.

1 - 2 3

2 - 4 5

3 10

4 88

5 65

7   TestCase 2.

1 / 2 3

2 - 4 5

3 - 6 7

4 261

5 61

6 81

7 71

...


[Output Example]

#1 13

#2 20

...












阅读(1434) | 评论(0) | 转发(0) |
0

上一篇:[20170617 PRO]

下一篇:[Intermediate 8] Maze

给主人留下些什么吧!~~