# 一个算法问题，100号乘客找到正确座位的概率

5 篇文章 0 订阅

有一个飞机有100个座位，飞机票被卖给了100个乘客，乘客必须顺序依照自己的编号就坐。如果自己的位置被别人占用了，就随机在剩下的空座位中找一个坐下。正常情况下，就是1号乘客坐1号座位，2号乘客坐2号座位，以此类推。

可是现在1号乘客是个疯子，他随便找了个座位坐下（不一定是1号座位）。现在问第100号乘客坐到自己座位的概率是多少？

1. 第100号座位必定会被人坐到，而且是在这100个乘客中的某个人。

2. 那么所有乘客坐到第100座位的概率之和应该是1.

依次分析 1号乘客坐到100号座位的概率是 0.01, 2号乘客坐到100号座位的条件是1号乘客占了2号的位子，2号选择了100号座位坐下，概率是(1/100)*(1/99), 3号乘客坐到 100 号座位的条件是前面2个人有人坐了他的位子，他去坐100号座位。否则他就要去坐3号自己的位置。

   def showList(la):
print("---------showList-----------")
print("[")
s = ''
for i in range(len(la)):
if i % 10 == 0:
s += "["
s += '{0:.5f}'.format(la[i])
if (i+1) % 10 == 0:
print(s + "]")
s = ''
else:
s += ", "
print(s+ "]")

seats = [0 for i in range(100)]

seats[0] = 1.0/len(seats)
for i in range(len(seats)-1):
for j in range(i):
temp = seats[j] * (1.0/(len(seats) - i))

seats[i] += temp

# 为确保总概率为1，目标位置的概率最好为1.0减去前面的人坐目标位置的概率

seats[len(seats)-1] = 1.0-sum(seats)
showList(seats)

---------showList-----------

[

[0.01000, 0.00010, 0.00010, 0.00011,0.00011, 0.00011, 0.00011, 0.00011, 0.00012, 0.00012]

[0.00012, 0.00012, 0.00013, 0.00013,0.00013, 0.00014, 0.00014, 0.00014, 0.00015, 0.00015]

[0.00015, 0.00016, 0.00016, 0.00017,0.00017, 0.00018, 0.00018, 0.00019, 0.00019, 0.00020]

[0.00020, 0.00021, 0.00021, 0.00022,0.00023, 0.00023, 0.00024, 0.00025, 0.00026, 0.00026]

[0.00027, 0.00028, 0.00029, 0.00030,0.00031, 0.00032, 0.00034, 0.00035, 0.00036, 0.00038]

[0.00039, 0.00041, 0.00043, 0.00044,0.00046, 0.00048, 0.00051, 0.00053, 0.00055, 0.00058]

[0.00061, 0.00064, 0.00067, 0.00071,0.00075, 0.00079, 0.00084, 0.00089, 0.00095, 0.00101]

[0.00108, 0.00115, 0.00123, 0.00132,0.00142, 0.00154, 0.00167, 0.00181, 0.00198, 0.00216]

[0.00238, 0.00263, 0.00292, 0.00327,0.00368, 0.00417, 0.00476, 0.00549, 0.00641, 0.00758]

[0.00909, 0.01111, 0.01389, 0.01786,0.02381, 0.03333, 0.05000, 0.08333, 0.16667, 0.50000]

]

---------showList-----------
[
[0.10000, 0.01111, 0.01389, 0.01786, 0.02381, 0.03333, 0.05000, 0.08333, 0.16667, 0.50000]

]

===================================

#----------------------------
def showList(la):
print("---------showList-----------")
print("[")
s = ''
for i in range(len(la)):
if i % 10 == 0:
s += "["
s += '{0:.5f}'.format(la[i])
if (i+1) % 10 == 0:
print(s + "]")
s = ''
else:
s += ", "
print(s+ "]")

# 求出第 peopleNum 个人（从1开始，人和座位编号是从0开始的）找到自己位置的概率，
# totalSeatsNum 是座位总数
def getPsit2TargetSeat(peopleNum, totalSeatsNum):
pSeats = [0 for i in range(peopleNum)]

pSeats[0] = 1.0/totalSeatsNum

for i in range(peopleNum - 1):
for j in range(i):
temp = pSeats[j] * (1.0/(totalSeatsNum - i))
pSeats[i] += temp

if peopleNum > 1:
pSeats[peopleNum - 1] = 1- sum(pSeats)
#showList(pSeats)
return pSeats[peopleNum - 1]

# 获取每个人坐到自己座位的概率的列表，totalSeatsNum是总座位数
def getProbilityOfEveryOneSetSelfSeat(totalSeatsNum):
p = []
for i in range(totalSeatsNum):
p.append(getPsit2TargetSeat(i+1, totalSeatsNum))
return p

SEATS_NUM = 10 # 总座位数
#T_NUM = 10
#print(getPsit2TargetSeat(T_NUM, SEATS_NUM))

showList(getProbilityOfEveryOneSetSelfSeat(SEATS_NUM))

--------------------------分割线

---------showList-----------
[
[0.10000, 0.90000, 0.88889, 0.87500, 0.85714, 0.83333, 0.80000, 0.75000, 0.66667, 0.50000]
]

• 0
点赞
• 0
评论
• 1
收藏
• 一键三连
• 扫一扫，分享海报

02-01 495

04-29 514
04-15 1415
11-27 1647
06-17 4734
03-18 1万+
09-23 2万+
04-04 3605
06-21 161
11-11 572
07-12 6352
10-04 160
06-14 4147