TopCoder problem "TheBlackJackDivOne" used in SRM 448 (Division I Level One)



Problem Statement

    

John and Brus are training for a card game tournament. There they will be playing Black Jack. Black Jack is played using a standard deck containing 52 distinct cards. Each card can be represented as a two-character string where the first character is the rank ('2' - '9' for ranks Two through Nine, 'T' for Ten, 'J' for Jack, 'Q' for Queen, 'K' for King and 'A' for Ace) and the second character is the suit ('S' for Spades, 'C' for Clubs, 'D' for Diamonds and 'H' for Hearts). For example, the Jack of Spades can be represented as "JS" and the Nine of Hearts as "9H".

Each time a player receives a card from the deck, his score increases by the card's value. Ranks Two through Ten have values of 2 - 10, respectively. Jacks, Queens and Kings each have a value of 10, and Aces have a value of 11.

Brus randomly shuffles the full card deck and gives John one or more cards from the top of the deck. You are given a String[] cards, where each element represents a single card given to John in this initial step. John will then take extra cards from the top of the deck, one by one, until his score is greater than or equal to 21. Return the expected number of extra cards that John will take.

 

Definition

    
Class:TheBlackJackDivOne
Method:expected
Parameters:String[]
Returns:double
Method signature:double expected(String[] cards)
(be sure your method is public)
    
 

Notes

-The returned value must be accurate to within a relative or absolute value of 1E-9.
 

Constraints

-cards will contain between 1 and 50 elements, inclusive.
-Each element of cards will contain exactly two characters, where the first character is '2'-'9', 'T', 'J', 'Q', 'K' or 'A', and the second character is 'S', 'C', 'D' or 'H'.
-All elements of cards will be distinct.
 

Examples

0)
    
{"JS"}
Returns: 2.105854341736695
1)
    
{"KD", "8S"}
Returns: 1.08
John will take the second extra card only if the first one is Two.
2)
    
{"KD", "2S", "2C", "2D", "2H"}
Returns: 1.0
The same situation, but here there are no Twos left in the deck.
3)
    
{"AS", "KS", "9S", "JC", "2D"}
Returns: 0.0
Here John's score is already more than 21.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=10615

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13902&pm=10615

Writer:

Vasyl[alphacom]

Testers:

PabloGilberto , ivan_metelsky , Chmel_Tolstiy

Problem categories:

Recursion, Search