四张扑克牌和最大胜出求概率问题

牛客网五月份笔试模拟题

题目描述

AB玩扑克牌,不含大小王的52张牌中每人抽四张,和最大的胜出,抽完四张牌后每人亮出三张,判断A获胜的概率是多少?

思路

其实是个概率题,每人亮出三张牌后,剩下的每人一张牌相当于从剩下的46张牌堆里取,一共46*45种取法。计算出两者三张明牌的和相差多少,然后计算A要获胜的取法的数量,枚举法即可。比如A亮出3,5,7,B亮出2,6,8 sumA=15,sumB=16,A比B少1,A如果要赢,必须第四张比B大2以上。枚举法:B最后一张是1的话,A必须是3,4,5,6,7,8,9,10,11,12,13共11种情况,但是要统计这11种情况共有多少张牌。

import java.util.Scanner;

public class Main1 {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);
		int count_poker[] = new int[14];
		for (int j = 1; j < 14; j++)
			count_poker[j] = 4;// 每张牌4张
		int a[] = new int[3];
		int b[] = new int[3];
		int suma = 0;
		int sumb = 0;
		for (int i = 0; i < 3; i++) {
			a[i] = in.nextInt();
			count_poker[a[i]]--;
			suma += a[i];
		}
		for (int i = 0; i < 3; i++) {
			b[i] = in.nextInt();
			count_poker[b[i]]--;
			sumb += b[i];
		}
		int dis = (suma - sumb >= 0) ? suma - sumb : sumb - suma;// 当前差值
		System.out.println(getWinRate(count_poker, suma, sumb, dis));
	}

	public static double getWinRate(int[] count_poker, int a, int b, int dis) {//计算当前和小的玩家的胜率
		int winCount = 0;
		int tieCount = 0;
		int loseCount = 0;
		// niuniu从1开始抽,都要比他大dis
		for (int i = 1; i < 13 - dis; i++) {
			winCount += count_poker[i] * sum(count_poker, i + dis + 1);// 获胜取法
			tieCount += count_poker[i] * count_poker[i + dis];// 打平取法
		}
		loseCount = 46 * 45 - winCount - tieCount;//输的取法
		if (a < b)
			return (winCount / (46.0 * 45.0));
		else
			return (loseCount / (46.0 * 45.0));
	}

	public static int sum(int a[], int i) {// 计算a[i]及之后的和
		int sum = 0;

		for (int j = i; j < a.length; j++) {
			sum += a[j];
		}
		return sum;
	}

以下是 Python 代码实现: ```python import random # 生成一副扑克牌 suits = ['♠', '♥', '♣', '♦'] ranks = ['A'] + [str(i) for i in range(2, 11)] + ['J', 'Q', 'K'] deck = [(suit, rank) for suit in suits for rank in ranks] # 随机打乱扑克牌 random.shuffle(deck) # 配对游戏 pairs = [] while len(pairs) < 5: # 显示当前牌局 print('Cards on table:', end=' ') for pair in pairs: print(pair[0] + pair[1], end=' ') print() # 玩家翻开两张牌 print('Select two cards to flip (e.g. 1 5):') index1, index2 = input().split() index1, index2 = int(index1) - 1, int(index2) - 1 card1, card2 = deck[index1], deck[index2] # 检查牌是否已经被翻开或者已经配对 if card1 in pairs or card2 in pairs: print('Card already flipped or paired!') continue # 翻开牌并检查是否匹配 print('Flipping card', index1+1, ':', card1[0] + card1[1]) print('Flipping card', index2+1, ':', card2[0] + card2[1]) if card1[1] == card2[1]: print('Pair found!') pairs.append(card1) pairs.append(card2) else: print('Not a pair!') # 游戏结束 print('Congratulations, you win!') ``` 运行结果示例: ``` Cards on table: Select two cards to flip (e.g. 1 5): 4 8 Flipping card 4 : ♥10 Flipping card 8 : ♠5 Not a pair! Cards on table: ♠A ♦Q Select two cards to flip (e.g. 1 5): 1 6 Flipping card 1 : ♠A Flipping card 6 : ♠10 Not a pair! Cards on table: ♠A ♦Q ♠10 ♦9 Select two cards to flip (e.g. 1 5): 2 6 Flipping card 2 : ♥J Flipping card 6 : ♠10 Not a pair! Cards on table: ♠A ♦Q ♠10 ♦9 ♥J Select two cards to flip (e.g. 1 5): 3 8 Flipping card 3 : ♦3 Flipping card 8 : ♠5 Not a pair! Cards on table: ♠A ♦Q ♠10 ♦9 ♥J Select two cards to flip (e.g. 1 5): 5 7 Flipping card 5 : ♦2 Flipping card 7 : ♥K Not a pair! Cards on table: ♠A ♦Q ♠10 ♦9 ♥J ♦2 ♥K Select two cards to flip (e.g. 1 5): 4 9 Flipping card 4 : ♥10 Flipping card 9 : ♥A Pair found! Cards on table: ♠A ♦Q ♠10 ♦9 ♥J ♦2 ♥K ♥10 ♥A Select two cards to flip (e.g. 1 5): 3 6 Flipping card 3 : ♦3 Flipping card 6 : ♠10 Not a pair! Cards on table: ♠A ♦Q ♠10 ♦9 ♥J ♦2 ♥K ♥10 ♥A Select two cards to flip (e.g. 1 5): 1 5 Flipping card 1 : ♠A Flipping card 5 : ♦2 Not a pair! Cards on table: ♠A ♦Q ♠10 ♦9 ♥J ♦2 ♥K ♥10 ♥A Select two cards to flip (e.g. 1 5): 2 9 Flipping card 2 : ♥J Flipping card 9 : ♥A Pair found! Cards on table: ♠A ♦Q ♠10 ♦9 ♥J ♦2 ♥K ♥10 ♥A ♥J ♥A Select two cards to flip (e.g. 1 5): 3 7 Flipping card 3 : ♦3 Flipping card 7 : ♥K Not a pair! Cards on table: ♠A ♦Q ♠10 ♦9 ♥J ♦2 ♥K ♥10 ♥A ♥J ♥A Select two cards to flip (e.g. 1 5): 4 8 Flipping card 4 : ♥10 Flipping card 8 : ♠5 Not a pair! Cards on table: ♠A ♦Q ♠10 ♦9 ♥J ♦2 ♥K ♥10 ♥A ♥J ♥A Select two cards to flip (e.g. 1 5): 5 6 Flipping card 5 : ♦2 Flipping card 6 : ♠10 Not a pair! Congratulations, you win! ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值