2005:Blackjack
keyword
確率 C++
概要
n組のデッキを使ったブラックジャックでディーラーのopenになっている1枚と自分の2枚の数字が与えられる。現状態で(補充などを考えない)勝っている確率を求める問題。
実装するだけ。よく考えたらA2枚の場合を除いて21を越える場合はないのだった。
static int cards[14]; inline int getCard(){ char c; while( c = getchar(), !( ('0'<=c&&c<='9') || ('A'<=c&&c<='Z') )); if( '0' <= c && c <= '9' ) return c&15; if( c == 'A' ) return 1; if( c == 'T' ) return 10; if( c == 'J' ) return 11; if( c == 'Q' ) return 12; if( c == 'K' ) return 13; return EOF; } inline int sub(int x){ return (x>21)?-1:x; } inline int eval(int x, int y){ x = min(10,x), y = min(10,y); if(x==1){ if(y==1) return 12; return max( sub(1+y),sub(11+y) ); } if(y==1){ return max( sub(1+x),sub(11+x) ); } return sub(x+y); } int main(){ int n; while(scanf("%d",&n), n){ int i, total = n*52-3, cnt = 0; FOR(i,1,14) cards[i] = 4*n; int a1 = getCard(), b1 = getCard(), b2 = getCard(); cards[a1]--; cards[b1]--; cards[b2]--; FOR(i,1,14)if(eval(i,a1) < eval(b1,b2)){ cnt += cards[i]; } printf("%.3f\%\n\n",100*(double)cnt/total); } return 0; }