#P9861. [CCC 2008 S4] Twenty-four
[CCC 2008 S4] Twenty-four
题目描述
Twenty-four is a popular card game designed to be played by four players. Each player is dealt a deck of cards, which are kept face down. On every turn, each of the four players turns over the top card of his or her deck, so that it is visible to all. The goal is to find an arithmetic expression using the values of the cards (with meaning , meaning , meaning , and K meaning ) that evaluates to the number . For example, for the example in the illustration, one possible expression would be:
The first player to find such an expression wins the turn, and adds all four cards to the bottom of his or her deck.
Each valid arithmetic expression must use all four cards, and must combine their values using addition, subtraction, multiplication, or division. Parentheses are allowed to specify precedence of operations. Juxtaposing cards to make multiple-digit decimal numbers is not allowed (e.g. you cannot place the cards and beside each other to make ). Non-integer quotients of division are also not allowed, even as a partial result (of a subexpression of the overall expression).
In some cases, the players may take a very long time to find an expression evaluating to . In fact, in some cases, no such expression exists. Your task is to determine, given four cards, an expression that evaluates to the largest number less than or equal to .
输入格式
The first line contains an integer indicating the number of card hands that follow. Each hand consists of four lines. Each of these lines is an integer indicating the value of a card.
输出格式
For each hand, output a line containing an integer n if the cards can be combined using arithmetic operators to evaluate to . The value should be the largest possible value amongst all possible arithmetic expressions using these cards, so long as .
3
3
3
3
3
1
1
1
1
12
5
13
1
24
4
21