vb.net 计算组合

  Private Function compute(ByVal n As Long, ByVal r As Long)
        Dim result As Decimal = 1
        Dim temp1 As Decimal = 1
        Dim temp2 As Decimal = 1
        If r = 0 Then
            result = 1
        Else
            'n(n-1).....(n-r+1)
            For i As Long = n To n - r + 1 Step -1
                temp1 *= i
            Next
            'r!
            For i As Long = r To 1 Step -1
                temp2 *= i
            Next
            result = temp1 / temp2
        End If
        Return result
    End Function

你可能感兴趣的:(VB.NET)