《Cracking the Coding Interview》——第7章:数学和概率论——题目1

2014-03-20 01:57

题目:玩篮球投篮,有两种玩法:要么1投1中,要么3投两中。你单次投篮中的概率是p,那么对于不同的p,哪种玩法胜率更高?

解法:第一种总是胜率更高,可以列不等式算算,结果发现是个恒不等式。

代码:

 1 // 7.1 Suppose you're playing a basketball game, you have two choices:
 2 // A: one shot one hit
 3 // B: three shots two hits
 4 // For what probability of p would you choose A or B.
 5 // 
 6 // Answer:
 7 // P(A) = p;
 8 // P(B) = C(3, 2) * p * p * (1 - p);
 9 // if P(A) < P(B), p < 3 * p * p * (1 - p)
10 // 1 < 3 * p * (1 - p)
11 // 3 * p * p - 3 * p + 1 < 0
12 // There's no root for this equation, thus for any p, 1 > 3 * p * (1 - p)
13 // Thus for any p, p > 3 * p * (1 - p)
14 // P(A)> P(B), choosing A will always be better.
15 int main()
16 {
17     return 0;
18 }
原文地址:https://www.cnblogs.com/zhuli19901106/p/3612763.html