Problem Statement
You are helping your brother with his homework assignment. His teacher gave him two distinct numbers x and y, and asked him to use those numbers to form as many different expressions as possible. Each expression must satisfy all of the following rules:
- The only allowed operators are '+', '-' and '*'.
- x and y must each appear exactly twice. No other numbers are allowed.
- The result of the expression must be equal to val.
In other words, each expression can be written in the form "a op1 b op2 c op3 d", where each of op1, op2 and op3 is '+', '-' or '*', and among the numbers a, b, c and d, exactly two are equal to x and the other two are equal to y. Please note that the unary minus is not allowed (see example 0). Expressions are calculated from left to right, and there is no operator precedence. For example, to calculate the result of "2 + 2 * 3 + 3", you would first calculate 2 + 2, then multiply the result by 3, and then add 3 to get 15.
Return the total number of different expressions that can be formed. Two expressions are considered different if their string notations (as described in the previous paragraph) are different. For example, the expressions "2 + 3 - 2 - 3", "2 - 2 + 3 - 3" and "2 - 3 - 2 + 3" are all different.
Definition
- Class:
- CountExpressions
- Method:
- calcExpressions
- Parameters:
- int, int, int
- Returns:
- int
- Method signature:
- int calcExpressions(int x, int y, int val)
- (be sure your method is public)
Constraints
- x and y will each be between -100 and 100, inclusive.
- x and y will be different.
- val will be between -100000000 and 100000000, inclusive.
Examples
7
8
16
Returns: 9
The possible expressions are: 8 + 8 + 7 - 7 8 + 7 + 8 - 7 7 + 8 + 8 - 7 8 + 8 - 7 + 7 8 + 7 - 7 + 8 7 + 8 - 7 + 8 8 - 7 + 8 + 7 8 - 7 + 7 + 8 7 - 7 + 8 + 8 Please note that the unary minus is not allowed, so "-7 + 7 + 8 + 8" is not a valid expression.
100
1
10000
Returns: 12
-100
100
2
Returns: 0
3
5
7
Returns: 5
The possible expressions are: 3 * 5 - 3 - 5 5 * 3 - 3 - 5 3 * 5 - 5 - 3 5 * 3 - 5 - 3 5 - 3 * 5 - 3
34
67
95
Returns: 0
12
24
36
Returns: 0
25
50
1000
Returns: 0
99
100
98010000
Returns: 6
48
-65
-153985
Returns: 1
-53
-64
-179712
Returns: 3
-61
-77
-286594
Returns: 3
-13
-52
0
Returns: 14
-87
43
43
Returns: 1
-96
-95
18335
Returns: 1
-35
-38
1444
Returns: 6
-87
-15
-113310
Returns: 1
73
-95
-497230
Returns: 1
-83
-31
-212598
Returns: 1
-80
-30
5760000
Returns: 6
-99
42
-1764
Returns: 2
-99 - (-99) - 42 * 42 -99 - 42 - (-99) * 42
-23
5
2640
Returns: 3
-84
-92
-649060
Returns: 3
-99
-22
-216106
Returns: 1
-72
-59
7765
Returns: 0
-82
58
-129
Returns: 0
-75
-92
-9202
Returns: 0
100
-100
100000000
Returns: 6
100
-100
-100000000
Returns: 0
There are no valid expressions.
2
3
6
Returns: 13
1
2
6
Returns: 15
-1
1
0
Returns: 36
100
-100
100
Returns: 6
100
-100
-100
Returns: 6
1
2
5
Returns: 17
-100
-99
9901
Returns: 4
-100
-50
100
Returns: 7
-100
-1
-1
Returns: 8
-100
-99
-200
Returns: 9
-100
-51
-200
Returns: 10
-100
-49
-200
Returns: 11
-4
-2
-4
Returns: 16
-100
-1
0
Returns: 19
-100
100
-10000
Returns: 18
-4
4
-16
Returns: 21
-4
-2
0
Returns: 20
-2
-1
0
Returns: 22
-100
100
0
Returns: 24
-2
2
4
Returns: 27
-2
0
4
Returns: 26
-2
2
0
Returns: 30
-2
2
-4
Returns: 33
0
2
4
Returns: 38
1
0
0
Returns: 76
100
0
1000
Returns: 0
2
1
6
Returns: 15
0
1
1
Returns: 36
0
99
99
Returns: 17
8
7
16
Returns: 9