| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11224 | Prefix |
|
| 12054 | Least Common Multiple |
|
Description
Infix notation: X + Y
- Operators are written in-between their operands. This is the usual way we write expressions. An expression such as
A * ( B + C ) / Dis usually taken to mean something like: "First add B and C together, then multiply the result by A, then divide by D to give the final answer."
Prefix notation (also known as "Polish notation"): + X Y
- Operators are written before their operands. The expressions given above are equivalent to
/ * A + B C D
Now, please write a program to convert the given expressions from prefix to infix.
Input
The first line contains a positive integer N, indicating the number of testcases in this input.
In the following N lines, each line contains a prefix expression.
In each prefix expression, there is a space between numbers and operators, and operators and operators.
Output
Output the infix expression and its answer of each given prefix expression.
Note that
- There is a space between numbers and operators, and operators and operators.
- If the answer is integer, there is no need to print decimal point. Otherwise, you should print only one digit after the decimal point.
- You have to print a '\n' at the end of each ouput.
- Add a pair of parentheses to wrap around each operator and its operands.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Calculate the least common multiple of multiple positive integers.
Definition of least common multiple: if m is a common multiple of a set of positive integers S, then for all elements i in S, i|m. The least common multiple is the smallest m satisfy the above condition.
Hint:
You should have already learned about how to calculate GCD by recursive or sequential ways, then we can calcute LCM of 2 numbers a and b by the result of GCD. Suppose D=gcd(a,b), and let a equals p*D and b equals q*D (p and q are relatively prime), then LCM of a and b should be p*q*D, or any other equivalent formats you prefer.
Input
Input consists of multiple test cases.
Each of them occupy 2 lines.
The first line contains a integer N, representing the # of positive integers.
The second line contains N positive integers. Numbers are seperated by a space.
It is guaranteed that
- the least common multiple of these numbers is less than 231.
- N <= 200
- Number of test cases <= 10
Output
For each test case, print out the least common multiple of these positive integers in one line.