POJ2005解题报告 计算概率

    技术2022-05-20  45

    Blackjack Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 1760 Accepted: 485

    Description

    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.

    Input

    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.

    Output

    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.

    Sample Input

    1 T A J 4 2 3 4 0

    Sample Output

    93.878% 21.951% 题意:给定n副扑克牌,现在你从中抽了两张,你的对手也抽了两张,你知道的一张牌,计算你能赢他的概率。。 简单的概率水题。。。 #include<iostream> using namespace std; int ret(char a) { int sum=0; if(a=='A') sum+=11; else if(a>'A') sum+=10; else sum+=a-48; return sum; } int gt(char a) { if(a<'A') return a-48; if(a=='A') return 14; if(a=='T') return 10; if(a=='K') return 13; if(a=='Q') return 12; if(a=='J') return 11; } int main() { int card,left,i; char lock[20],a,b,c; while(cin>>card&&card) { memset(lock,0,sizeof(lock)); cin>>c>>a>>b; int sum=0,soc=0,w=0; if(a=='A'&&b=='A') sum=12; else { sum+=ret(a); sum+=ret(b); } soc+=ret(c); for(i=2;i<=10;i++) if(soc+i<sum) lock[i]=1; for(i=11;i<=13;i++) if(soc+10<sum) lock[i]=1; if(soc<11&&soc+11<sum) lock[14]=1; else if( soc==11&&soc+1<sum) lock[14]=1; left=52*card-3; for(i=2;i<15;i++) if(lock[i]) w+=4; w*=card; if(lock[gt(a)]) w--; if(lock[gt(b)])w--; if(lock[gt(c)])w--; printf("%0.3lf%%/n/n",double(w)*100.0/double(left)); } }

    最新回复(0)