2. 100个人排队上飞机,飞机上有100个座位,每个人对应一个座位,队首的人是个瞎子,上飞机后随便坐,后面的人都尽量找自己的座位,如果已经被占,则随机做,问第100个人坐到自己座位的概率
另写一程序计算,效率很低
Module Module1
Sub Main()
Console.WriteLine(Count(7, 7))
Console.ReadKey()
End Sub
Public Function Count(ByVal totalPersons As Integer, ByVal targetPerson As Integer, Optional ByVal currentPerson As Integer = 0, Optional ByVal container() As Boolean = Nothing) As Integer
Dim result = 0
If currentPerson = 0 Then
For firstPerson = 0 To totalPersons - 1
Dim currentContainer(totalPersons - 1) As Boolean
currentContainer(firstPerson) = True
result += Count(totalPersons, targetPerson, currentPerson + 1, currentContainer)
Next
Else
If currentPerson = targetPerson - 1 Then
If container(targetPerson - 1) = False Then
result += 1
End If
Return result
End If
If container(currentPerson) = False Then
Dim currentContainer(totalPersons - 1) As Boolean
container.CopyTo(currentContainer, 0)
currentContainer(currentPerson) = True
result += Count(totalPersons, targetPerson, currentPerson + 1, currentContainer)
Else
For personIndex = 1 To totalPersons - 1 - currentPerson
Dim index = 0
For person = 0 To totalPersons - 1
If container(person) = False Then
index += 1
If personIndex = index Then
Dim currentContainer(totalPersons - 1) As Boolean
container.CopyTo(currentContainer, 0)
currentContainer(person) = True
result += Count(totalPersons, targetPerson, currentPerson + 1, currentContainer)
Continue For
End If
End If
Next
Next
End If
End If
Return result
End Function
End Module