首页 > 专题系列 > Java解POJ > POJ 2005 Blackjack [解题报告] Java
2013
11-10

POJ 2005 Blackjack [解题报告] Java

Blackjack

问题描述 :

In the Blackjack card game, the player and the dealer are dealt two cards initially. One of dealer’s cards is dealt face up and is known to the player but the other one is dealt face down. Given the two initial cards you are dealt and the dealer’s face-up card, you are asked to compute the probability that your two-card hand is better than the dealer’s two-card hand. This is not simple: the probability changes as the game is played because cards are dealt from the decks without replacement. To simplify the problem, we will only compute the probability when the cards are first dealt from the decks. That is, no cards have been dealt from the decks before.

In this game, an Ace has a value of 1 or 11 (chosen by the person holding the cards), the face cards (K, Q, J) have a value of 10, and the values of the remaining cards are given by their numerical values. The player wins against the dealer if:

  • the total value of the player’s hand does not exceed 21; and
  • the total value of his hand is higher than that of the dealer or the total value of the dealer’s hand exceeds 21.

The value of an Ace is chosen to maximize the total value without exceeding 21. If we are only interested in two-card hands, it is impossible for the total value to exceed 21.

Skilled players remember which cards have already been dealt and make decisions accordingly. To make this difficult, many casinos use multiple decks of cards to play the game. Each deck has 52 cards, 4 of each of A, K, Q, J, T (10), 9, …, and 2.

输入:

The input consists of a number of test cases. Each test case starts with a line containing a positive integer n (n <= 10) indicating the number of decks used. This is followed by a line containing 3 characters (separated by a space) in the set {A, K, Q, J, T, 9, ..., 2}, representing the dealer's face-up card and your two cards in the hand. In each case, assume that the n decks have been shuffled together randomly. The end of input is specified by n = 0.

输出:

For each hand dealt, print on a line the probability of winning as a percentage, rounded to 3 decimal places. Separate the output of each case by a blank line.

样例输入:

1
T A J
4 
2 3 4
0

样例输出:

93.878%

21.951%

解题代码:

//* @author: 
import java.util.*;
public class Main
{
 
 public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   int tot[]=new int[12],t[]=new int[4];
   char ts[]=new char[5];
    int sum,s1,s2,win,tem;
    while (sc.hasNext()) {
       int n=sc.nextInt();
       if(n==0) break;
        for (int i=2;i< 10;i++) tot[i]=4*n;
        tot[10]=16*n;tot[11]=4*n;
        sum=52*n-3;win=0;
        for (int i=1;i<=3;i++) {
            ts=sc.next().toCharArray();
            if (ts[0]=='T'||ts[0]=='J'||ts[0]=='Q'||ts[0]=='K') t[i]=10;
            else if (ts[0]=='A') t[i]=11;
            else t[i]=ts[0]-48;
            tot[t[i]]--;
        }
        s1=t[1];s2=t[2]+t[3];
        if (s2>21) s2-=10;
        for (int i=2;i<=11;i++) {
            tem=s1+i;if (tem>21) tem-=10;
            if (tem< s2) win+=tot[i];
        }
        System.out.printf("%.3f%%\n\n",100.0*win/sum);
    }
  }
}

  1. Gucci New Fall Arrivals

    This is really nice to know. I hope it will be successful in the future. Good job on this and keep up the good work.

  2. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?

  3. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.