例如,共有N=5个景点,每个景点连接的下一个景点分别是2,4,5,5,2。
景点号 | 1 | 2 | 3 | 4 | 5 |
下一景点号 | 2 | 4 | 5 | 5 | 2 |
则他可以从2号景点出发,最多可以游玩2号、4号、5号三个景点。
程序代码如下:
Private Sub Command1_Click()Dim a(1 To 100) As Integer, d(1 To 100) As Integer
Dim jd As String, m As String, c As Integer, i As Integer
Dim s As Integer, p As Integer, k As Integer, ans As Integer
jd = Text1.Text + ","
s = 0: c = 0
For i = 1 To Len(jd)
m = Mid(jd, i, 1)
If m <> "," Then
①
Else
c = c + 1: a(c) = s: s = 0
End If
Next i
ans = 0: k = 0
For i = 1 To c
For k = 1 To c
d(k) = 0
Next k
If d(i) = 0 Then
p = i
Do While p <= c
If d(p) = 0 Then
k = k + 1: d(p) = k
Else
If y > ans Then ans = y
k = 0
Exit Do
End If
Loop
End If
Next i
Text2.Text = Str(ans)
End Sub