若現在有 5張撲克牌,點數分別是 1點至 5點,各張牌的點數都不同,並且 5張牌已依點數由小到大排列。選手若要改變排列順序,只能用「換牌」動作。假設一個「換牌」的動作,可以將順序中的第
1張牌(首牌)與其他牌「交換」位置,形成 1個新的牌組順序。我們現在用 1個長度為 5的字串,表示 1個牌組順序。例如以字串「12345」,表示這 5張牌現在依其點數由小到大排列。如果我們進行第 1次「換牌」動作,假設將「牌 1」(首牌)與「牌 5」交換,就形成新的牌組順序「52341」。如果要進行第 2次的「換牌」,就將「牌 5」(首牌)與其他牌交換,即可形成下一個牌組順序。現在給一個「目的地牌組順序」,請從牌組順序「12345」開始,用最多 10次的「換牌」動作,最後轉換成「目的地牌組順序」。
輸入說明:第 1行為給定的「目的地牌組順序」字串(長度為 5)。
輸出說明:第 1行輸出起始牌組順序「 12345」,最後 1行輸出「目的地牌組順序」。第 2行起依序輸出每次「換牌」後的牌組順序。輸入檔案 1:【檔名:in1.txt】
21534
輸入檔案 2:【檔名:in2.txt】
31524
輸出檔案:【檔名:out.txt】
12345
32145
42135
52134
12534
21534
12345
52341
32541
42531
12534
21534
31524
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in9-1.txt" For Input As #1
Open App.Path & "\in9-2.txt" For Input As #2
Open App.Path & "\out9.txt" For Output As #3
Input #1, n
Call A1(n)
Print #3, " "
Input #2, n
Call A1(n)
Close
Close
Close
End
End Sub
Sub A1(a)
Dim S(5), org, ch2(5)
org = Split("0 1 2 3 4 5")
Print #3, org(1) & org(2) & org(3) & org(4) & org(5)
For i = 1 To 5
m = Mid(a, i, 1)
ch2(i) = m
Next
For i = 2 To 5
If ch2(i) = org(i) Then S(i) = "V"
Next
Do
For i = 2 To 5
If org(1) = ch2(1) Then
For j = 2 To 5
If S(j) <> "V" Then t = org(1): org(1) = org(j): org(j) = t: GoTo EX:
Next
Else
If org(1) = ch2(i) And S(i) <> "V" Then t = org(1): org(1) = org(i): org(i) = t: S(i) = "V": Exit For
End If
Next
EX:
Print #3, org(1) & org(2) & org(3) & org(4) & org(5)
For i = 1 To 5
If ch2(i) = org(i) Then S(i) = "V"
Next
kk = 0
For i = 1 To 5
If S(i) = "V" Then kk = kk + 1
Next
Loop Until kk = 5
End Sub
Dim X(5)
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in9-1.txt" For Input As #1
Open App.Path & "\in9-2.txt" For Input As #2
Open App.Path & "\out9.txt" For Output As #3
Line Input #1, ans
For i = 1 To 5
X(i) = i
Next i
List1.AddItem "12345"
Call ABC(ans, "")
List1.AddItem ans
For i = 0 To List1.ListCount - 1
Print #3, List1.List(i)
Next i
Print #3,
List1.Clear
Line Input #2, ans
For i = 1 To 5
X(i) = i
Next i
List1.AddItem "12345"
Call ABC(ans, "")
List1.AddItem ans
For i = 0 To List1.ListCount - 1
Print #3, List1.List(i)
Next i
Close #3
Close #2
Close #1
End
End Sub
Sub ABC(A, B)
If A = B Then
Else
For i = 2 To 5
T = X(1)
X(1) = X(i)
X(i) = T
B = ""
For j = 1 To 5
B = B & X(j)
Next j
If re(B) = False Then List1.AddItem B
Next i
End If
End Sub
Function re(A) As Boolean
P = False
For i = 0 To List1.ListCount - 1
If List1.List(i) = A Then P = True: Exit For
Next i
re = P
End Function