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

2014-03-20 01:59

题目:有n只蚂蚁在正n边形的n个顶点,同时以同速率开始沿着边走。每只蚂蚁走的方向是随机的,那么这些蚂蚁至少有两只发生碰撞的概率是多少。

解法:只有所有蚂蚁都往一个方向走才不会碰撞,所以不碰的概率就是2/2^n,碰的概率就用1减去喽。

代码:

1 // 7.2 n ants are standing on the vertices of an n-edged equilateral polygon, they start walking at the same time, same speed.
2 // Every ant chooses randomly a direction to go. What is the probability of a collision.
3 // p = 2 / 2^n = 2^-(n - 1)
4 int main()
5 {
6     return 0;
7 }

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3612765.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值