-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExp3.java
83 lines (69 loc) · 1.7 KB
/
Exp3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public class Exp3 {
public static void main(String[] args){
long m = Long.parseLong(args[0]);
binomialDistribution B = new binomialDistribution(0.5,m);
System.out.println(B);
}
}
interface iterable{
double getSmaller();
double getBigger();
}
interface calculable {
void do_math();
double getResult();
}
abstract class distribution{
public abstract double maximumExpection();
}
class binomialDistribution extends distribution{
long m;
double p;
public binomialDistribution(double p,long n){
this.m = n;
this.p = p;
}
@Override
public double maximumExpection(){
calculable cal = new calculator(1,m);
cal.do_math();
return cal.getResult();
}
@Override
public String toString(){
return "C("+2*m + "," + m + ") * "+p+"^" + 2*m + " = " + this.maximumExpection();
}
}
class calculator implements iterable, calculable {
private long start;
private long end;
private long cur_start;
private long cur_end;
private double result = 1.0;
public calculator(long start,long end){
this.start = start;
this.end = end;
cur_end = this.end;
cur_start = this.start;
}
public void do_math(){
while(cur_end>=cur_start){
if(result>1)
result*=getSmaller();
else
result*=getBigger();
}
}
double appendent(long i){
return 0.25*(this.end+i)/i;
}
public double getSmaller(){
return appendent(cur_end--);
}
public double getBigger(){
return appendent(cur_start++);
}
public double getResult(){
return result;
}
}