import java.util.Arrays; public class Main{ double[][] probabilities; int numMines = 4; //The number of mines you have double firstProbability = .5; //The probability that a mine has gold at first. 50% in example problem. Express as a number between 0 and 1 double probabilityAfterFound = .9; //The probability that a mine has gold once one gold has been found. 90% in example problem. probabilities = new double[numMines + 1][numMines+1]; probabilities[0][0]=1; for(int mine = 1; mine <= numMines; mine++){ probabilities[mine][0] = probabilities[mine-1][0]*(1-firstProbability); probabilities[mine][1] = probabilities[mine-1][0]*(firstProbability) + probabilities[mine-1][1]*(1-probabilityAfterFound); for(int numGolds = 2; numGolds <= numMines; numGolds++){ probabilities[mine][numGolds] = probabilities[mine-1][numGolds-1]*probabilityAfterFound + probabilities[mine-1][numGolds]*(1-probabilityAfterFound); } } for(int mine = 0; mine <= numMines; mine++){ } double expectedGolds = 0; for(int numGolds = 0; numGolds<=numMines; numGolds++){ expectedGolds+=numGolds*probabilities[numMines][numGolds]; } System.out.println("You expect to find gold at least once " + ((1-probabilities[numMines][0])*100) + "% of the time"); } }
Standard input is empty
[1.0, 0.0, 0.0, 0.0, 0.0] [0.5, 0.5, 0.0, 0.0, 0.0] [0.25, 0.3, 0.45, 0.0, 0.0] [0.125, 0.155, 0.315, 0.405, 0.0] [0.0625, 0.078, 0.171, 0.324, 0.36450000000000005] You expect to find gold 2.85 times. You expect to find gold at least once 93.75% of the time