例如,共有N=5个景点,每个景点连接的下一个景点分别是 2,4,2,3,1。
景点号 | 1 | 2 | 3 | 4 | 5 |
下一景点号 | 2 | 4 | 2 | 3 | 1 |
则他可以从2号景点出发,最多可以游玩2号、3号、4号三个景点。程序代码如下:
Private Sub Command1_Click()
Dim a(1 To 100) As Integer, d(1 To 100) As Integer '数组 a 存放下一景点号
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
For k=1 To 100 d(k)=0
Next k
ans=0: k=0
For i=1 To c '枚举起点
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