#P9861. Largest Expression Value Under 24
Largest Expression Value Under 24
Largest Expression Value Under 24
In this problem, you are given four cards represented as strings. The cards can be one of the following: a number (e.g. 2, 3, …, 10) or one of the letters A (representing 1), J (11), Q (12), and K (13). You must use the four cards – each exactly once – to form a valid arithmetic expression using the operators: addition (+
), subtraction (-
), multiplication (*
), and division (/
). Parentheses can be used to specify operation precedence. Note that juxtaposing cards to form multi-digit numbers is not allowed and each division operation must yield an integer result (i.e. non‐integer intermediate results are forbidden).
Your objective is to output an expression that evaluates to the largest number less than or equal to 24. If an expression equaling 24 exists, then 24 is the best possible answer. Otherwise, among all valid expressions that yield a value ≤ 24, choose one whose value is maximum. If there are multiple optimal answers, any one is acceptable.
All arithmetic operations in the expression should follow the standard integer arithmetic rules. All formulas with any mathematical notation (such as the conversion of cards) must use LaTeX. For example, the card conversion should be noted as: \(A=1,\;J=11,\;Q=12,\;K=13\).
inputFormat
The input consists of a single line containing four space-separated card representations. Each card is either a number (2, 3, ... ,10) or one of the letters A, J, Q, K.
outputFormat
Output one valid arithmetic expression that uses all four cards exactly once and evaluates to the largest number less than or equal to 24, following the rules described above. If multiple expressions yield the maximum value, output any one of them.
sample
A K Q J
((A*K)-J)*Q