Problem 5(8%):傳統數學問題的解決
子題1:給定若干個數字,將它們依序排成一個頭、尾相接的環形。如果我們從某一數字開
始點名,每次點名後再往後跳2 個數字繼續點名(中間間隔1 個未被點名的數字)。已經被點
到名的數字不可再點,直到剩下最後一個「未點名」的數字時才停止。請問最後的數字為何?
輸入說明:
第1 行依序給定最多20 個要點名的數字,各數字之間以逗號隔開。
第2 行有1 個數字,是「開始點名」的數字。
輸出說明:
第1 行輸出最後一個未點名的數字。
輸入範例:【檔名:in-5-1.txt】
5, 3, 7, 11, 4, 2, 1, 8, 9
8
輸出範例:【檔名:out-5-1.txt】
9
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Line Input #1, T
X = Split(T, ", ")
For i = 0 To UBound(X)
List1.AddItem X(i)
Next i
Input #1, Y
W = find(Y)
Do
List1.RemoveItem ((W + 2) Mod List1.ListCount)
Loop Until List1.ListCount = 1
Print #2, List1.List(0)
Close #2
Close #1
End
End Sub
Function find(A)
For i = 0 To List1.ListCount - 1
If List1.List(i) = A Then find = A
Next i
End Function
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Line Input #1, m
s = Split(m, ",")
For i = 0 To UBound(s):
List1.AddItem s(i):
Next
Input #1, n
Call A1(n)
Close
Close
End
End Sub
Sub A1(a)
Dim k
For i = 0 To List1.ListCount - 1
If List1.List(i) = a Then k = a
Next
Do
List1.RemoveItem ((k + 2) Mod List1.ListCount)
Loop Until List1.ListCount = 1
Print #2, List1.List(0)
End Sub