python 面试、笔试题目(基础篇二)

一、生日悖论问题:当房间中人数n超过23时,那么该房间中有两个人的生日相同的可能性是一半以上。请用Python设计程序证明。

解析:

两个人生日不同的概率:364/365

n个人互相组合:n*(n-1) / 2

所以n个人互相组合并且生日不同:(364/365)* (n*(n-1)/2)

def birth_age(num):
    import math
    prop = 1 - math.pow((364.0 / 365.0), (num * (num - 1) / 2))
    return prop


print(birth_age(23))

猜你喜欢

转载自blog.csdn.net/qq_40771567/article/details/109640261