2009年2月23日 星期一

Q10056: What is the Probability?

// Run time: 0.010
#include <stdio.h>
double calculate(int n, double p, int index);
int main()
{
int num; // 輸入組數
int n; // 人數
double prob; // Probability
int index; // which player

scanf("%d", &num);
while (num--)
{
scanf("%d%lf%d", &n, &prob, &index);

if (!prob)
printf("0.0000\n");
else
printf("%.4lf\n", calculate(n, prob, index));
}

return 0;
}

double calculate(int n, double p, int index)
{
int i;
double arr[n];
double non_win = 1-p;
double weight = 1-p;
double prod;

arr[0] = p; // initial

for ( i = 1; i < n; i++)
{
prod = non_win*p; // 前面都沒發生的機率 * 事件發生的機率
arr[i] = prod;
non_win = non_win*weight;
}

double sum = 0;

for ( i = 0; i < n; i++)
sum += arr[i];

return arr[index-1]/sum;
}

沒有留言: