The collection of interesting mathematical problems found in this type of tasks when necessary from the given numbers and arithmetic signs to make the expression, giving something a specific number. An example of such tasks: "Make of the numbers 4, 1, 8, and 7 math symbols an expression equal to 24". The answer can serve as an expression: "(8-4) * (7-1)". Unfortunately, some authors admit the error and offer an unsolvable problem, so their head turned to the programmers.

You need to write a program that determines if you can build from a given set expression is equal to the number N or not. Since this is only a prototype, it is enough to write a program which works only with sets of 4 numbers, and the number N is always equal to 24.

Valid arithmetic operators: addition, subtraction, multiplication, division, brackets.

Input: an array of 4 integers from 1 to 9.

Output: true (if set of you can build expressions equal to 24) or false (if the set of such expressions can not be built).

Example 1.

At the entrance: [4, 1, 8, 7]

Output: true

Explanation: (8-4) * (7-1) = 24

he began to write.... kind of like that, Yes, and it worked, but came with outstretched brackets. and I can not figure out how to make them right. after all, if it is opened or closed, and location relative to the arifma.the sign is changed. here is the code which work, but not working. tell me what to do. and is it possible to somehow simplify this mountain cycles:

You need to write a program that determines if you can build from a given set expression is equal to the number N or not. Since this is only a prototype, it is enough to write a program which works only with sets of 4 numbers, and the number N is always equal to 24.

Valid arithmetic operators: addition, subtraction, multiplication, division, brackets.

Input: an array of 4 integers from 1 to 9.

Output: true (if set of you can build expressions equal to 24) or false (if the set of such expressions can not be built).

Example 1.

At the entrance: [4, 1, 8, 7]

Output: true

Explanation: (8-4) * (7-1) = 24

he began to write.... kind of like that, Yes, and it worked, but came with outstretched brackets. and I can not figure out how to make them right. after all, if it is opened or closed, and location relative to the arifma.the sign is changed. here is the code which work, but not working. tell me what to do. and is it possible to somehow simplify this mountain cycles:

```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a, b, c, d;
char x, y, z, e, q, w;
a = 1;
b = 2;
c = 3;
d = 4;
int[] arr = new int[4];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
char[] charr = new char[6];
charr[0] = '+';
charr[1] = '-';
charr[2] = '/';
charr[3] = '*';
charr[4] = '(';
charr[5] = ')';
for (int i = 0; i < 4; i++) {
a = arr[i];
for (int j = 0; j < 4; j++) {
if (i != j) {
b = arr[j];
for (int k = 0; k < 4; k++) {
if (k != j & k != i) {
c = arr[k];
for (int l = 0; l < 4; l++) {
if (l != j & l != k & l != i) {
d = arr[l];
for (int m = 4; m < 5; m++) { // cycle characters
for (int n = 0; n < 6; n++) {
if (n != m ) {
for (int o = 0; o < 6; o++) {
if (o != n & o != m) {
for (int p = 0; p < 6; p++) {
if (p != o & p != n & p != m) {
for (int r = 0; r < 6; r++) {
if (r != m & r != n & r != o & r != p) {
for (int s = 0; s < 6; s++) {
if (s != m & s != n & s != o & s != p & s != r) {
System.out.println (charr[s] + a + charr[m] );
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
```

asked March 19th 20 at 08:34

2 answers

answered on March 19th 20 at 08:36

I'm not an expert on Java, but the answer is **recursion**.

Can do without it, emulation stack in the array, but this is essentially the same.

This solves the huge problem of nesting of identical cycles.

Also very advise you to study the format of RPN - it will greatly simplify how exhaustive search and computation. For example, your answer

Can do without it, emulation stack in the array, but this is essentially the same.

This solves the huge problem of nesting of identical cycles.

Also very advise you to study the format of RPN - it will greatly simplify how exhaustive search and computation. For example, your answer

`(8-4) * (7-1)`

will look like an array `8, 4, -, 7, 1, -, *`

. The brackets disappear. The problem is reduced to the permutation of the 4 digits in the 7 positions and sorting operations in the remaining. Not all options are valid, but how exactly to optimize, and whether it be you or just to work - decide for yourself. answered on March 19th 20 at 08:38

I'm not an expert on Java, but the answer is **python**.

A fragment of the output (there are too many):

```
from itertools import permutations, product
from operator import add, sub, mul, truediv
def f(data):
oops, res = (add, sub, mul, truediv), {}
sign = {add: '+', sub: '-', mul: '*', truediv: '/'}
for t in permutations(data):
for order in product([0, 1, 2], [0, 1], [0]):
for ops in product(oops, repeat=3):
try:
ll, ss = list(map(float, t)), list(map(str, t))
for i, op in zip(order, ops):
ll[i:i + 2] = [op(*ll[i:i + 2])]
ss[i:i + 2] = [f'({ss[i]}{sign[op]}{ss[i + 1]})']
if ll[0].is_integer():
res[int(ll[0])] = ss[0][1:-1]
except ZeroDivisionError:
pass
return res
for x, s in sorted(f([4, 1, 8, 7]).items()):
print(f'{x:>4} : {s}')
```

A fragment of the output (there are too many):

```
20 : 7+(8+(1+4))
21 : 7*(8-(1+4))
22 : ((7/4)+1)*8
23 : ((7-4)*8)-1
24 : (7-1)*(8-4)
25 : ((7-4)*8)+1
26 : (8*4)-(7-1)
27 : (7*(8-4))-1
28 : 7*(8-(1*4))
29 : (7*(8-4))+1
```

Find more questions by tags Java