Here you go
Пользователь:
Hedger (IP-адрес скрыт)
Дата: 27.06.2017 19:34
В условии задачи (хотя бы) можно опустить, так как два куска не могут иметь длину больше единицы.
что один из кусков будет больше 1м.
Function rope(ByVal numPlays As Long) As Double
'------------------------------------------------------------------------
' Rope
' Author: Sergey Mikhaylov
' Purpose: Calculate the probability via Monte-Carlo simulation
' Parameters: numPlays - number of simulations
' 2 - rope length
' Revision history: 26.06.2017
'-------------------------------------------------------------------------
Dim i As Long, j As Long
Dim x1 As Double, x2 As Double
Dim len1 As Double, len2 As Double, len3 As Double
j = 0
Randomize
For i = 1 To numPlays
' Calculate the coordinates of random points in the (0,2)
x1 = 2 * Rnd() ' x1 coordinate
x2 = 2 * Rnd() ' x2 coordinate
' The length of the pieces
If (x1 < x2) Then
len1 = x1
len2 = x2 - x1
len3 = 2 - x2
Else
len1 = x2
len2 = x1 - x2
len3 = 2 - x1
End If
' Condition: The length of one piece is more than one
If ((len1 > 1) Or (len2 > 1) Or (len3 > 1)) Then
j = j + 1
End If
Next i
' Probability
If (numPlays <> 0) Then
rope = j / numPlays
Else
rope = -3
End If
End Function