Inpur four integers in 1 to 10, you may use plus, minus, multiply and divid to get 24. Note that every integer should be used once and only once. Brackets are of no limit, and the result should exactly be 24 once you calculate by hand. (Calculation in computer will lead to a tolerance to the computer precision.)
You need to return a format and a vector with the same four integers, such that you can print the expression.
E.g., input x = [5 5 7 7]
you may return f = '(%d*%d)-(%d/%d)'
and z = [5 5 7 7]
so 24 is obatined by eval(sprintf(f,z)).
The answer is not necessary unique, e.g., other acceptable answers are
f = '(%d*%d)-(%d*%d)'
z = [7 7 5 5]
and f = '(%d-%d)*(%d+%d)'
z = [7 5 7 5]
Solution Stats
Problem Comments
3 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers4
Suggested Problems
-
Choose the best fitting dominoes
251 Solvers
-
middleAsColumn: Return all but first and last element as a column vector
653 Solvers
-
Removing rows from a matrix is easy - but what about inserting rows?
279 Solvers
-
Find the definite integral of various linear functions
62 Solvers
-
248 Solvers
More from this Author12
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
This problem has several flaws.
1. The first assert() statement in each test should be "assert(ischar(f))' rather than 'assert(isstring(f))'
2. The expression for 'w' in each test should be using '&' operators rather than '|' operators
Thanks to william.
Case 2 fails because of a typo in the harness.
The typo, on Case 2's sprintf line:
Cases 1, 3, 4 and 5 have s(s>'9'|s<'0')=' ';
Case 2 has s(s>=9'|s<'0')=' ';
With the opening quote missing, MATLAB reads 9' as the number 9 (transposed), not the character '9'. Every printable character has a code of 9 or higher, so the whole string gets blanked. Then str2num returns [], the length check fails, and it drops into the catch block.