星期天,小龙来到动物园游玩,园内共有N个景点(可在10个以上),每个景点都有一个标号,标号为1至N。现在只知道每个景点有一条路连接下一个景点。小龙想知道,根据这些道路信息,从其中一个景点出发,最后再回到出发景点,最多能游玩多少个景点。
例如,共有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