您的位置:首页 > 其它

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

2014-03-20 01:59 417 查看
2014-03-20 01:57

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

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

代码:

// 7.1 Suppose you're playing a basketball game, you have two choices:
// A: one shot one hit
// B: three shots two hits
// For what probability of p would you choose A or B.
//
// Answer:
// P(A) = p;
// P(B) = C(3, 2) * p * p * (1 - p);
// if P(A) < P(B), p < 3 * p * p * (1 - p)
// 1 < 3 * p * (1 - p)
// 3 * p * p - 3 * p + 1 < 0
// There's no root for this equation, thus for any p, 1 > 3 * p * (1 - p)
// Thus for any p, p > 3 * p * (1 - p)
// P(A)> P(B), choosing A will always be better.
int main()
{
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐