Problem:
Problem A: Blackjack

Brian Jones is an undergraduate student at Advanced College of Metropolitan. Recently he was
given an assignment in his class of Computer Science to write a program that plays a dealer of
blackjack.

Blackjack is a game played with one or more decks of playing cards. The objective of each player
is to have the score of the hand close to 21 without going over 21. The score is the total points
of the cards in the hand. Cards from 2 to 10 are worth their face value. Face cards (jack, queen
and king) are worth 10 points. An ace counts as 11 or 1 such a way the score gets closer to
but does not exceed 21. A hand of more than 21 points is called bust, which makes a player
automatically lose. A hand of 21 points with exactly two cards, that is, a pair of an ace and a
ten-point card (a face card or a ten) is called a blackjack and treated as a special hand.

The game is played as follows. The dealer first deals two cards each to all players and the dealer
himself. In case the dealer gets a blackjack, the dealer wins and the game ends immediately. In
other cases, players that have blackjacks win automatically. The remaining players make their
turns one by one. Each player decides to take another card (hit) or to stop taking (stand) in
his turn. He may repeatedly hit until he chooses to stand or he busts, in which case his turn
is over and the next player begins his play. After all players finish their plays, the dealer plays
according to the following rules:

Hits if the score of the hand is 16 or less.

Also hits if the score of the hand is 17 and one of aces is counted as 11.

Stands otherwise.

Players that have unbusted hands of higher points than that of the dealer win. All players that
do not bust win in case the dealer busts. It does not matter, however, whether players win or
lose, since the subject of the assignment is just to simulate a dealer.

By the way, Brian is not good at programming, thus the assignment is a very hard task for him.

So he calls you for help, as you are a good programmer.
Your task is to write a program that counts the score of the dealer’s hand after his play for each
given sequence of cards.

Input

The first line of the input contains a single positive integer N , which represents the number of
test cases. Then N test cases follow.

Each case consists of two lines. The first line contains two characters, which indicate the cards
in the dealer’s initial hand. The second line contains eight characters, which indicate the top
eight cards in the pile after all players finish their plays.

A character that represents a card is one of A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q and K, where A is
an ace, T a ten, J a jack, Q a queen and K a king.

Characters in a line are delimited by a single space.

The same cards may appear up to four times in one test case. Note that, under this condition,
the dealer never needs to hit more than eight times as long as he or she plays according to the
rule described above.

Output

For each case, print on a line “blackjack” if the dealer has a blackjack; “bust” if the dealer
busts; the score of the dealer’s hand otherwise.

Sample Input

4
5 4
9 2 8 3 7 4 6 5
A J
K Q J T 9 8 7 6
T 4
7 J A 6 Q T K 7
2 2
2 3 4 K 2 3 4 K

Output for the Sample Input

18
blackjack
21
bust

Solution in JAVA (judge verdict: Wrong Answer)


import java.util.*;
import java.math.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.math.BigInteger.*;
import static java.lang.Character.*;

class AOJ2010 {
	public void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(); sc.nextLine();
		while(N-- > 0) {
			char[] s = sc.nextLine().toCharArray();
			char[] t = sc.nextLine().toCharArray();
			int ace = 0;
			int points = 0;
			boolean hasTenPointsCard = false;
			for(int i = 0; i < s.length; i += 2)
				if(isDigit(s[i]))
					points += s[i] - '0';
				else if(s[i] == 'A') {
					points += 11;
					ace++;
				}
				else {
					points += 10;
					hasTenPointsCard = true;
				}
			boolean bust = false;
			boolean bj = false;
			for(int i = 0; i <= t.length; i += 2) {
				if(points == 21 && ace > 0 && hasTenPointsCard) {
					bj = true;
					break;
				}
				if(i == t.length || points > 16 && ace == 0)
					break;

				if(isDigit(t[i]))
					points += t[i] - '0';
				else if(t[i] == 'A') {
					points += 11;
					ace++;
				}
				else {
					points += 10;
					hasTenPointsCard = true;
				}
				while(ace > 0 && points > 21) {
					ace--;
					points -= 10;
				}
				if(points > 21) {
					bust = true;
					break;
				}
			}
			System.out.println(bj ? "blackjack" : bust ? "bust" : points);
		}
	}
}

public class Main {
	public static void main(String... args) {
		new AOJ2010().run();
	}

	public static void debug(Object... os) {
		System.err.println(java.util.Arrays.deepToString(os));
	}
}

class Scanner {
	final java.util.Scanner sc;

	public double nextDouble() {
		return Double.parseDouble(sc.next());
	}

	public Scanner(java.io.InputStream is) {
		this.sc = new java.util.Scanner(is);
	}

	public boolean hasNext() {
		return sc.hasNext();
	}

	public String next() {
		return sc.next();
	}

	public int nextInt() {
		return Integer.parseInt(sc.next());
	}

	public String nextLine() {
		return sc.nextLine();
	}

	public long nextLong() {
		return Long.parseLong(sc.next());
	}
}