版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/PZHU_CG_CSDN/article/details/80873894
链接:https://www.nowcoder.com/questionTerminal/2b101eacfaf641988eae013115015d54?toCommentId=1451496
来源:牛客网
牛牛与妞妞闲来无聊,便拿出扑克牌来进行游戏。游戏的规则很简单,两个人随机抽取四张牌,四张牌的数字和最大的取胜(该扑克牌总张数为52张,没有大小王,A=1,J=11,Q=12,K=13,每种数字有四张牌),现在两人已经分别亮出了自己的前三张牌,牛牛想要知道自己要赢得游戏的概率有多大。
输入描述:
输入包含两行,第一行输入三个整数a1,b1,c1(1≤a1,b1,c1≤13),表示牛牛亮出的扑克牌。第二行输入三个整数a2,b2,c2(1≤a2,b2,c2≤13),表示妞妞所亮出的扑克牌。
输出描述:
输出一个数字x(保留4位小数),表示牛牛获胜的概率。
示例:
输入
3 5 7
2 6 8
输出
0.3995
分析:一共还剩下 46 张牌,可以通过排列组合知识求出从 46 张牌中取出两张,一人一张所有可能的总数,即 A(46,2) = 46*45 = 2070。然后跑双重循环,暴力枚举这 46 张牌,每次取两张,统计牛牛赢的次数,最后牛牛赢的次数除以总次数就是牛牛赢的概率。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
map<int,int> arr;
int sum1 = 0, sum2 = 0;
void init(){
for(int i = 1;i <= 13;i++)//每个点数的牌都有 4 种花色
arr[i] = 4;
int a,b,c;
cin>>a>>b>>c;
sum1 = a + b + c;
arr[a]--;
arr[b]--;
arr[c]--;
cin>>a>>b>>c;
sum2 = a + b + c;
arr[a]--;
arr[b]--;
arr[c]--;
}
int main(void){
init();
int cnt = 0,index = 0;
int a[100];
map<int,int>::iterator it;
for(it = arr.begin();it != arr.end();it++){//将剩下的牌全部取出来
for(int i = 0;i < it->second;i++){
a[index++] = it->first;
}
}
for(int i = 0;i < index;i++) //暴力枚举
for(int j = 0;j < index;j++){
if(j != i){
int temp1 = a[i];
int temp2 = a[j];
if(sum1 + temp1 > sum2 + temp2){
cnt++;
}
}
}
printf("%.4f\n",(double)(cnt)/(2070));//A(46,2) = 46*45 = 2070 从 46 张牌中取两张,一人一张所有可能
return 0;
}