View difference between Paste ID: NHWgK1fZ and mRrr8cLG
SHOW: | | - or go back to the newest paste.
1
import java.util.Random;
2
3
public class Test {
4
	
5
	public static Random rand = new Random();
6
	
7
	public static void main(String[] args) {
8
		double[] p = new double[6];
9
		p[0] = 0;
10-
		p[1] = 0.1;
10+
		p[1] = 0.2;
11-
		p[2] = 0.2;
11+
		p[2] = 0.4;
12-
		p[3] = 0.3;
12+
		p[3] = 0.6;
13-
		p[4] = 0.4;
13+
		p[4] = 0.8;
14
		p[5] = 1.0;
15
		
16
		double[] dp = new double[6];
17-
		dp[0] = 1;
17+
		dp[0] = 0;
18
		dp[1] = 0;
19
		dp[2] = 0;
20
		dp[3] = 0;
21
		dp[4] = 0;
22
		dp[5] = 0;
23
		
24
		long total = 0;
25
		
26
		for(int i = 0; i < 10000000; i++) {
27
			total += getHitScore(genHits(p));
28
		}
29
		
30
		System.out.println(total);
31
		System.out.println(((double)total)/10000000);
32
		
33
		int i;
34
		int n = 5;
35-
		int ans = 0;
35+
		double ans = 0;
36
		
37
		for(i = 1; i <= n; i++)
38
		    dp[i] = (dp[i-1] + p[i-1]) * p[i];   
39
		for(i = 1; i <= n; i++)
40
		    ans+= 2 * dp[i] + p[i];
41
		
42
		System.out.println(ans);
43
	}
44
	
45
	private static String genHits(double[] p) {
46
		String rtn = "";		
47
		for(int i = 1; i < p.length; i++) {			
48
			if(rand.nextDouble() < p[i]) {
49
				rtn += "o";	//hit
50
			} else {
51
				rtn += "x";	//miss
52
			}
53
		}
54
		
55
		return rtn;
56
	}
57
	
58
	private static int getHitScore(String str) {
59
		
60
		int score = 0;
61
		
62
		int streak = 0;
63
		for(int i = 0; i < str.length(); i++) {
64
			if(str.charAt(i) == 'o') {
65
				streak++;
66
			} else {
67
				score += streak + streak;
68
				streak = 0;
69
			}
70
		}
71
		
72
		score += streak + streak;
73
		
74
		return score;
75
	}
76
77
}