已知C++中目标命中概率,计算A获胜的概率
假设有两个玩家A和B,他们都在争取点球来赢得比赛。已知四个整型变量a、b、c、d,A先获得点球的概率为a/b,B先获得点球的概率为c/d。
先获得点球的一方将赢得比赛,根据题意,程序必须找到A赢得比赛的概率。
输入
a = 10, b = 20, c = 30, d = 40
输出
probability is 0.5333
输入
a = 1, b = 2, c = 10, d = 11
输出
probability is 0.523
下面程序中使用的方案如下:
输入四个整型变量a、b、c、d的值。
从总概率中减去B获胜的概率,我们将得到A获胜的最终概率。
e * (1 / (1 - (1 - f) * (1 - f)))
其中,e是A获胜的概率,f是B获胜的概率。
显示A赢得比赛的概率。
算法
Start Step 1→ Declare function to calculate the probability of winning double probab_win(int a, int b, int c, int d) Declare double e = (double)a / (double)b Declare double f = (double)c / (double)d return (e * (1 / (1 - (1 - f) * (1 - f)))) Step 2→ In main() Declare variable as int a = 10, b = 20, c = 30, d = 40 Call probab_win(a, b, c, d) Stop
示例
#include <bits/stdc++.h>
using namespace std;
// calculate the probability of winning the match
double probab_win(int a, int b, int c, int d){
double e = (double)a / (double)b;
double f = (double)c / (double)d;
return (e * (1 / (1 - (1 - f) * (1 - f))));
}
int main(){
int a = 10, b = 20, c = 30, d = 40;
cout<<"probability is "<<probab_win(a, b, c, d);
return 0;
}输出
运行以上代码将生成以下输出:
probability is 0.5333
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP