#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 AA meaning 11, JJ meaning 1111, QQ meaning 1212, and K meaning 1313) that evaluates to the number 2424. For example, for the example in the illustration, one possible expression would be:

((A×K)J)×Q((A \times K) - J) \times Q

((1×13)11)×12((1 \times 13) - 11) \times 12

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 22 and 44 beside each other to make 2424). 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 2424. 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 2424.

输入格式

The first line contains an integer 1N51 \leq N \leq 5 indicating the number of card hands that follow. Each hand consists of four lines. Each of these lines is an integer 1C131 \leq C \leq 13 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 nn. The value nn should be the largest possible value amongst all possible arithmetic expressions using these 44 cards, so long as n24n \leq 24.

题目大意

有一个算 2424 点的游戏,每局游戏都会给你 44 个整数(1(1 \leq 每个数 13\leq 13,表示你所需要用其来凑出 2424 点。你可以将其中的两个数通过加减乘除的运算,得到一个新数,一直重复如此的操作,直到你剩下一个整数,那便是你游戏的答案。

现在,你需要根据给出的那 44 个整数,来计算出一个整数 nn,使其最接近 2424,同时也不能超出 2424,问 nn 是多少。

你一共玩了 N(1N5)N(1 \leq N \leq 5) 局这样的游戏,请你求出每局游戏对应的 nn

3
3
3
3
3
1
1
1
1
12
5
13
1
24
4
21