题目链接
参考博客:
题意:
给你n种硬币,每次抛所有的硬币并移除字朝上的硬币,当只剩下一种硬币的时候,该种硬币为Lucky Coins,求每种硬币能成为Lucky Coins的概率;
#include <bits/stdc++.h>
using namespace std;
const int maxn = 15;
int n;
double num[maxn],ans[maxn],p[maxn];
double count_die(int x,int y)//第x种硬币第y次投全部remove的概率
{
return pow(1-pow(p[x],y),num[x]);
}
double count_live(int x,int y)//第x种硬币第y次投仍未全被remove的概率
{
return 1-count_die(x,y);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(ans,0,sizeof(ans));
for(int i=0; i<n; i++)
{
scanf("%lf %lf",&num[i],&p[i]);//num[i]每种硬币的个数,p[i]为其概率
}
if(n==1)//特判
{
printf("1.000000\n");
}
else
{
for(int i=0; i<100; i++)
{
for(int j=0; j<n; j++)
{
double tmp=1;
for(int k=0; k<n; k++)
{
if(k==j)
continue;
tmp*=count_die(k,i);
}
ans[j]+=(count_live(j,i)-count_live(j,i+1))*tmp;//(count_live(j,i)-count_live(j,i+1))表示第j次该种硬币未被remove而j+1次时被remove,整个式子表示该种硬币为幸运硬币的概率;
}
}
for(int i=0; i<n; i++)
{
printf("%.6f%c",ans[i],i==n-1?'\n':' ');
}
}
}
}
本文介绍了一种通过编程解决特定硬币游戏问题的方法,该游戏涉及多种类型的硬币,并且目标是计算每种硬币成为最后幸存者的概率。文中提供了一个C++实现方案,详细解释了如何使用概率论原理来解决这个问题。
525

被折叠的 条评论
为什么被折叠?



