-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA. Expression
More file actions
61 lines (49 loc) · 1.95 KB
/
A. Expression
File metadata and controls
61 lines (49 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
A. Expression
time limit per test1 second
memory limit per test256 megabytes
Petya studies in a school and he adores Maths. His class has been studying arithmetic expressions. On the last class the teacher wrote three positive integers a, b, c on the blackboard. The task was to insert signs of operations '+' and '*', and probably brackets between the numbers so that the value of the resulting expression is as large as possible. Let's consider an example: assume that the teacher wrote numbers 1, 2 and 3 on the blackboard. Here are some ways of placing signs and brackets:
1+2*3=7
1*(2+3)=5
1*2*3=6
(1+2)*3=9
Note that you can insert operation signs only between a and b, and between b and c, that is, you cannot swap integers. For instance, in the given sample you cannot get expression (1+3)*2.
It's easy to see that the maximum value that you can obtain is 9.
Your task is: given a, b and c print the maximum value that you can get.
Input
The input contains three integers a, b and c, each on a single line (1 ≤ a, b, c ≤ 10).
Output
Print the maximum value of the expression that you can obtain.
Examples
InputCopy
1
2
3
OutputCopy
9
InputCopy
2
10
3
OutputCopy
60
import java.util.Scanner;
public class Expression {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the three integers a, b, c
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
// Calculate all possible results of expressions
int result1 = a + b + c;
int result2 = a * b * c;
int result3 = a + (b * c);
int result4 = (a + b) * c;
int result5 = a * (b + c);
// Find the maximum of all results
int maxResult = Math.max(Math.max(Math.max(result1, result2), Math.max(result3, result4)), result5);
// Output the maximum value
System.out.println(maxResult);
scanner.close();
}
}