夺冠概率模拟
时间: 1ms 内存:128M
描述:
足球比赛具有一定程度的偶然性,弱队也有战胜强队的可能。假设有甲、乙、丙、丁四个球队。根据他们过去比赛的成绩,得出每个队与另一个队对阵时取胜的概率表:甲 乙 丙 丁甲 - 0.1 0.3 0.5乙 0.9 - 0.7 0.4丙 0.7 0.3 - 0.2丁 0.5 0.6 0.8 -数据含义:甲对乙的取胜概率为0.1,丙对乙的胜率为0.3,...现在要举行一次锦标赛。双方抽签,分两个组比,获胜的两个队再争夺冠军。(参见【1.jpg】)请你进行10万次模拟,计算出甲队夺冠的概率。注意:请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
输入:
输出:
示例输入:
示例输出:
提示:
参考答案(内存最优[0]):
#include <iostream>
#include <cmath>
#include <iomanip>
#define DIF 0.00001
#define COUNT 100000
using namespace std;
double JtoY=0.1;
double JtoB=0.3;
double JtoD=0.5;
double YtoJ=0.9;
double YtoB=0.7;
double YtoD=0.4;
double BtoJ=0.7;
double BtoY=0.3;
double BtoD=0.2;
double DtoJ=0.5;
double DtoY=0.6;
double DtoB=0.8;
double RESULT;
int count=COUNT;
int main()
{
double dfs(char a=0,char b=0);
count=0;
cout<<setiosflags(ios::fixed)<<setprecision(5)<<dfs()<<endl;
return 0;
}
double dfs(char a=0,char b=0)
{
if(count<=0)
{
if(b=='Y')
{
return JtoY*BtoD*JtoB+JtoY*DtoB*JtoD;
}
else
{
return JtoB*YtoD*JtoY+JtoB*DtoY*JtoD;
}
}
count>>=1;
RESULT=0.5*dfs('J','Y')+0.5*dfs('J','B');
return RESULT;
}
参考答案(时间最优[0]):
#include <iostream>
#include <cmath>
#include <iomanip>
#define DIF 0.00001
#define COUNT 100000
using namespace std;
double JtoY=0.1;
double JtoB=0.3;
double JtoD=0.5;
double YtoJ=0.9;
double YtoB=0.7;
double YtoD=0.4;
double BtoJ=0.7;
double BtoY=0.3;
double BtoD=0.2;
double DtoJ=0.5;
double DtoY=0.6;
double DtoB=0.8;
double RESULT;
int count=COUNT;
int main()
{
double dfs(char a=0,char b=0);
count=0;
cout<<setiosflags(ios::fixed)<<setprecision(5)<<dfs()<<endl;
return 0;
}
double dfs(char a=0,char b=0)
{
if(count<=0)
{
if(b=='Y')
{
return JtoY*BtoD*JtoB+JtoY*DtoB*JtoD;
}
else
{
return JtoB*YtoD*JtoY+JtoB*DtoY*JtoD;
}
}
count>>=1;
RESULT=0.5*dfs('J','Y')+0.5*dfs('J','B');
return RESULT;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。