Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Test {
- public static Random rand = new Random();
- public static void main(String[] args) {
- double[] p = new double[6];
- p[0] = 0;
- p[1] = 0.2;
- p[2] = 0.4;
- p[3] = 0.6;
- p[4] = 0.8;
- p[5] = 1.0;
- double[] dp = new double[6];
- dp[0] = 0;
- dp[1] = 0;
- dp[2] = 0;
- dp[3] = 0;
- dp[4] = 0;
- dp[5] = 0;
- long total = 0;
- for(int i = 0; i < 10000000; i++) {
- total += getHitScore(genHits(p));
- }
- System.out.println(total);
- System.out.println(((double)total)/10000000);
- int i;
- int n = 5;
- double ans = 0;
- for(i = 1; i <= n; i++)
- dp[i] = (dp[i-1] + p[i-1]) * p[i];
- for(i = 1; i <= n; i++)
- ans+= 2 * dp[i] + p[i];
- System.out.println(ans);
- }
- private static String genHits(double[] p) {
- String rtn = "";
- for(int i = 1; i < p.length; i++) {
- if(rand.nextDouble() < p[i]) {
- rtn += "o"; //hit
- } else {
- rtn += "x"; //miss
- }
- }
- return rtn;
- }
- private static int getHitScore(String str) {
- int score = 0;
- int streak = 0;
- for(int i = 0; i < str.length(); i++) {
- if(str.charAt(i) == 'o') {
- streak++;
- } else {
- score += streak + streak;
- streak = 0;
- }
- }
- score += streak + streak;
- return score;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement