简体   繁体   中英

How to resolve this 3x3 matrix with equations requirements

I was in an interview today and I got the following exercice :
write a program to find all the 3x3 matrix X for each element x varying from 0 to 100

A   B   C

D   E   F

G   H   I

that meet the following requirements

A + B - C = 4
+   -   -
D - E * F = 4
/   *   -
G + H + I = 4
=   =   =
4   4   4

write a program in Java.

It is not really clear what your question is, but this seems quite straightforward to simply bruteforce by trying all choices in a sensible order.

For example, this Python code:

for G in range(1,4+1):
    for H in range(4+1-G):
        I = 4 - H - G
        for A in range(0,4+1):
            D = G*(4-A)
            if not 0<=D<=100:
                continue
            for E in range(100+1):
                for F in range(100+1):
                    if D-E*F==4:
                        for B in range(100+1):
                            C=A+B-4
                            if 0<=C<=100:
                                if B-E*H==4:
                                    if C-F-I==4:
                                        print A,B,C
                                        print D,E,F
                                        print G,H,I
                                        print A+B-C,D-E*F,G+H+I,A+D/G,B-E*H,C-F-I

finds the following 4 solutions:

0 10 6
4 6 0
1 1 2
4 4 4 4 4 4
2 7 5
4 3 0
2 1 1
4 4 4 4 4 4
1 8 5
6 2 1
2 2 0
4 4 4 4 4 4
2 6 4
4 1 0
2 2 0
4 4 4 4 4 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM