牛客网五月份笔试模拟题
题目描述
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;
}