抛硬币,正面向上的概率为p反面向上的概率为1 - p。求抛多少次可以得到一次正反和一次反正。
http://www.bnuoj.com/bnuoj/problem_show.php?pid=29064
正反和反正的概率是一样的,都是p * (1 - p)。
题目给出的概率p都是3位小数,我们可以先将它乘以1000得到一个整数。
这样正反的概率就是(p * 1000 * (1 - p) * 1000 )/ (1000 * 1000),也就是说要抛(1000 * 1000) / (p * 1000 * (1 - p) * 1000)次就可以抛一次正反。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int N;
char a[10];
int main(){
scanf("%d", &N);
while(N--){
scanf("%s", a);
int x = 0;
for(int i=2; i<=4; i++){
x *= 10;
x += a[i] - '0';
}
// printf("%d\n", x);
double y = (double)x * (1000 - (double)x);
// printf("%.2f\n", y);
printf("%.2f\n", 1000.0 * 1000.0 / y);
}
return 0;
}