One Saturday evening you are playing a game of online Scrabble. Your opponent is a very good player, but this time you managed to win. After a brief conversation, you are told: “I am clearly better than you, but one game is simply not enough to prove it.” Your opponent then makess the following bet: “If we play 10 games, you will win less than 5 … and this will happen every time, even if we try this 10 times in a row!”.
You will solve a more general problem using the following parameters:
– an int trials denoting the number of meetings in which a set of games is played.
– an int games denoting the number of games that are to be played in each meeting.
– an int winsNeeded denoting the number of victories you need in one of the meetings to win the bet.
– an int winChance denoting the probability in percent of winning one particular game.
Return a double between 0 and 1, denoting the probability you have to win the bet.
[java]
public class ScrabbleBet {
public double estimate(int trials, int games, int winsNeeded, int winChance) {
System.out.println(trials+":"+games+":"+winsNeeded+":"+winChance);
double one_game_op_not_lose=0;
for (int i = winsNeeded; i <= games; i++) {
one_game_op_not_lose+=combination(games, i)*Math.pow(((double)winChance/100),i)*Math.pow(((double)(1-(double)winChance/100)),(games-i));
}
one_game_op_not_lose=1-one_game_op_not_lose;
return 1-Math.pow(one_game_op_not_lose, trials);
}
//nCr
public long combination(int n,int r){
return fact(n)/(fact(n-r)*fact(r));
}
//n!
public long fact(int n){
if (n<=1) {
return 1;
}
return n*fact(n-1);
}
}
[/java]
コメント