2. 100个人排队上飞机,飞机上有100个座位,每个人对应一个座位,队首的人是个瞎子,上飞机后随便坐,后面的人都尽量找自己的座位,如果已经被占,则随机做,问第100个人坐到自己座位的概率

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值