Problem Statement
The game of tennis has a rather unusual scoring system. It can be simplified to the following rule: "If a player has at least four points, and is at least two points ahead of his opponent, that player wins the game." For example, 4-1 and 6-4 are winning scores, whereas 3-0 and 5-4 are not.
We can generalize this class of point systems by introducing two variables, pointsToWin and pointsToWinBy. The new rule is "If a player has at least pointsToWin points, and is at least pointsToWinBy points ahead of his opponent, that player wins the game." For example, the common practice of taking "best two out of three" falls into this class, where pointsToWin = 2 and pointsToWinBy = 1.
You would like to know, given a particular point system and the skills of two players, the probability of an upset. An "upset" is defined as a game where a player of lesser skill beats a player of greater skill. Given an
Definition
- Class:
- PointSystem
- Method:
- oddsOfWinning
- Parameters:
- int, int, int
- Returns:
- double
- Method signature:
- double oddsOfWinning(int pointsToWin, int pointsToWinBy, int skill)
- (be sure your method is public)
Constraints
- pointsToWin will be between 1 and 10, inclusive.
- pointsToWinBy will be between 1 and 5, inclusive.
- skill will be between 1 and 50, inclusive.
Examples
2
1
40
Returns: 0.352
This is the 'best two out of three' game. There are exactly three possible ways for an upset to occur: 1) The worse player scores two consecutive points, with probability 0.4*0.4 = 0.16 2) The better player scores one point, and then the worse player scores two consecutive points, with probability 0.6*0.4*0.4 = 0.096 3) The worse player scores one point, the better player scores one point, and then finally the worse player a second point, with probability 0.4*0.6*0.4 = 0.096 Thus, the total probability is 0.16+0.096+0.096 = 0.352
4
5
50
Returns: 0.4999999999999998
When the probability is 50, clearly each player is the same, so each player has a 50% chance of winning.
3
3
25
Returns: 0.035714285714285705
10
5
48
Returns: 0.3949566562885549
1
4
19
Returns: 0.003018293842361875
9
1
24
Returns: 0.00938185073388358
2
4
43
Returns: 0.2446403891804691
2
4
3
Returns: 9.149506804307481E-7
10
1
39
Returns: 0.16261164304381354
3
4
42
Returns: 0.21566777043166588
8
3
27
Returns: 0.014182677355033809
10
2
35
Returns: 0.07753434802621584
4
5
36
Returns: 0.05331136440582318
3
5
40
Returns: 0.11636363636363636
7
4
48
Returns: 0.41541557608193425
3
4
19
Returns: 0.003018293842361875
10
3
15
Returns: 4.9649834934940344E-5
6
1
33
Returns: 0.11714026864751065
6
4
24
Returns: 0.006268999307536835
2
5
44
Returns: 0.23044321230549097
7
5
38
Returns: 0.07496708917277026
10
3
27
Returns: 0.008354424610246745
2
3
50
Returns: 0.49999999999999983
1
5
6
Returns: 1.0595379673221927E-6
4
3
46
Returns: 0.37719720065808476
8
5
15
Returns: 3.9430677631904765E-5
3
4
11
Returns: 2.332971226964399E-4
5
4
22
Returns: 0.004985284180102935
8
3
33
Returns: 0.054031166460224746
1
3
13
Returns: 0.0033252610867261986
6
5
6
Returns: 3.752506923513088E-7
2
2
16
Returns: 0.035010940919037205
6
5
25
Returns: 0.00361208055840164
4
4
2
Returns: 1.7346649546680006E-7
6
2
5
Returns: 2.922912033839778E-6
1
2
34
Returns: 0.20972423802612483
2
2
10
Returns: 0.012195121951219511
6
5
2
Returns: 4.681331729841816E-10
9
5
42
Returns: 0.15345039304298383
8
2
40
Returns: 0.1985732411392001
1
2
7
Returns: 0.005633478960680618
5
3
17
Returns: 0.003036053386891745
9
5
8
Returns: 8.332180533119887E-8
9
2
31
Returns: 0.04118848281606164
2
1
11
Returns: 0.033638
7
5
10
Returns: 3.895021845893313E-6
5
5
50
Returns: 0.4999999999999998
1
4
17
Returns: 0.0017567893141372188
1
5
4
Returns: 1.2558672548311644E-7
9
5
31
Returns: 0.0118228182263838
9
2
12
Returns: 3.0240524779784205E-5
7
4
9
Returns: 5.333811408104796E-6
5
3
8
Returns: 6.912134402628339E-5
1
1
1
Returns: 0.01
8
2
33
Returns: 0.07177242867828629
10
1
3
Returns: 4.254066587798827E-11
2
5
10
Returns: 1.693480101608806E-5
4
5
29
Returns: 0.011240584072091355
9
5
13
Returns: 6.054000139275781E-6
8
1
30
Returns: 0.05001254005377599
9
1
2
Returns: 1.0764362527481045E-11
8
1
4
Returns: 3.274352606107888E-8
1
5
12
Returns: 4.7148989837743444E-5
7
4
34
Returns: 0.050787140789304845
10
2
48
Returns: 0.426213526838083
9
2
38
Returns: 0.14099043987057447
4
4
12
Returns: 3.456560423663357E-4
2
4
27
Returns: 0.018370094769793885
10
5
21
Returns: 2.734850032311632E-4
5
2
49
Returns: 0.47267592578044787
6
2
8
Returns: 4.554672826806804E-5
6
1
1
Returns: 4.425434338348E-10
9
1
30
Returns: 0.0402769360542546
8
1
20
Returns: 0.004239749709824001
7
1
10
Returns: 9.928548640000001E-5
4
5
15
Returns: 1.7111471023167378E-4
8
5
36
Returns: 0.0460605973614878
6
2
30
Returns: 0.06331923062068968
5
5
48
Returns: 0.40126103608143454
9
1
30
Returns: 0.0402769360542546
7
4
42
Returns: 0.19956702395603976
2
3
19
Returns: 0.012741965446776889
8
3
11
Returns: 1.9364280273286726E-5
9
5
32
Returns: 0.015558722804942745
7
3
31
Returns: 0.04316016924324729
10
5
50
Returns: 0.4999999999999991
10
5
49
Returns: 0.44694092401748997
1
1
50
Returns: 0.5
10
5
23
Returns: 6.424438832898403E-4