Answer the question
In order to leave comments, you need to log in
Is there a problem with brackets?
In collections of entertaining mathematical problems, there is such a type of problem when it is necessary to compose an expression from given numbers and arithmetic signs that gives some given number. An example of such a task: "Make an expression equal to 24 from the numbers 4, 1, 8, 7 and arithmetic signs." The answer might be: "(8-4) * (7-1)". Unfortunately, some authors make mistakes and offer an unsolvable problem, so their boss turned to programmers.
It is necessary to write a program that determines whether it is possible to build an expression equal to the number N from a given set or not. Since this is only a prototype, it is enough to write a program that 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 an expression equal to 24 can be built from the given set) or false (if such an expression cannot be built from the given set).
Example 1.
Input: [4, 1, 8, 7]
Output: true
Explanation: (8-4) * (7-1) = 24
started writing.... it seems like something worked, but here arose with substitution of brackets. and I can't figure out how to put them in the right way. after all, if it opens or closes, then the location relative to the arithm. sign changes. Here is the code that turned out, but does not work. tell me how to do it. and is it possible to somehow simplify this mountain of 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++) { // цикл символов
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] );
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
Answer the question
In order to leave comments, you need to log in
I'm not a Java expert, but the answer is recursion .
It is possible without it, with stack emulation in an array, but it is essentially the same.
This solves the problem of huge nesting of identical loops.
I also strongly advise you to study the RPN format - it will greatly simplify both the enumeration of options and calculations. For example, your response (8-4) * (7-1)
will look like an array 8, 4, -, 7, 1, -, *
. The brackets disappear. The task is reduced to permutation of 4 digits in 7 positions and enumeration of operations in the remaining ones. Not all options are valid, but how exactly to optimize, and whether you need it or just to make it work - decide for yourself.
I'm not a Java expert, but the answer is python .
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}')
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question