在ASCII CODE 中,每個字元需要使用 8 bit來存資料。當檔案只包含0123456789AB 十二種字元時,可二進制重新編碼以節省空間,假設新編碼如下:
表3.2.1
二進制
|
字元
|
00
|
A
|
01
|
B
|
100
|
0
|
101
|
1
|
1100
|
2
|
1101
|
3
|
11100
|
4
|
11101
|
5
|
111100
|
6
|
111101
|
7
|
111110
|
8
|
111111
|
9
|
例如編碼10100110001 對應到的字元為 1A2B。在猜數字的遊戲過程中,會選定不重複的數字排列當做答案,再由玩家來猜數字,再算出幾A幾B,其遊戲過程可用表3.2.1的編碼紀錄(答案為何,幾A幾B如何算出,不是這次題目考慮的範圍)。例如玩家猜數字6789,算出(0A0B),則把這過程6789 (0A0B)以1111001111011111101111111000010001編碼,為了讓選手方便對照剛剛的編碼,我們將6789 (0A0B) 編碼拆解成111100 (6) 111101 (7) 111110 (8) 111111 (9) 100 (0) 00 (A) 100 (0) 01 (B);
若玩家猜數字1253,算出(2A1B),這過程1253(2A1B)以表3.2.1的方式編碼紀錄為
101 1100 11101 1101
1100 00 101 01
(輸入檔案會省略空白,空白的存在是為了方便讀題)
輸入說明:
第1行的數字n代表有幾筆資料要測試,而n的值介於1和5之間,之後每行為0和1所組成的編碼字串,字串長度<=34,對應到一次猜數字的遊戲過程。在測試檔案中,每個編碼字串均可正確的對應到編碼表中的編碼。
輸出說明:
從第1行起每行將輸入之編碼字串,轉成玩家猜的數字及其幾A幾B的結果。(輸出英文字均為大寫,選手請注意。數字和其幾A幾B的結果以”,”分開。)
輸入檔案1:【檔名:in1.txt】
4
1111001111011111101111111000010001
101110011101110111000010101
11100110111001011110001
111011100110111100110100
輸入檔案2:【檔名:in2.txt】
2
1011100100111111110000
1011100110111100110001
輸出範例:【檔名:out.txt】
6789,0A0B
1253,2A1B
4321,4B
5234,3A
1209,2A
1234,2B
Private Sub Form_Load()
回覆刪除Dim strr, formstr(12), ans As String
Dim symbol As Integer
Me.Hide
formstr(1) = "00"
formstr(2) = "01"
formstr(3) = "100"
formstr(4) = "101"
formstr(5) = "1100"
formstr(6) = "1101"
formstr(7) = "11100"
formstr(8) = "11101"
formstr(9) = "111100"
formstr(10) = "111101"
formstr(11) = "111110"
formstr(12) = "111111"
Open App.Path & "\in1.txt" For Input As #1
Open App.Path & "\in2.txt" For Input As #2
Open App.Path & "\out.txt" For Output As #3
Input #1, tempa
Input #2, tempb
mycount = tempa + tempb
For i = 1 To mycount
If i <= tempa Then Line Input #1, strr
If i > tempa Then Line Input #2, strr
Do
For ii = 12 To 1 Step -1
If Mid(strr, 1, Len(formstr(ii))) = formstr(ii) Then
If ii = 1 Then
ans = ans & "A": Exit For
ElseIf ii = 2 Then
ans = ans & "B": Exit For
Else
ans = ans & ii - 3: Exit For
End If
End If
Next
symbol = symbol + 1
If symbol = 4 Then ans = ans & ","
strr = Right(strr, Len(strr) - Len(formstr(ii)))
Loop Until Len(strr) = 0
Print #3, ans
ans = ""
symbol = 0
If i = tempa Then Print #3,
Next
Close #2
Close #1
End
End Sub
Dim ans, ans1, ans2, x, y As String
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in1.txt" For Input As #1
Open App.Path & "\in2.txt" For Input As #2
Open App.Path & "\out.txt" For Output As #3
Input #1, a
For n = 1 To a
Line Input #1, x
y = Len(x)
ans = ""
Call test
Next
Print #3,
Input #2, a
For n = 1 To a
Line Input #2, x
y = Len(x)
ans = ""
Call test
Next
Close #1
Close #2
End
End Sub
Public Sub test()
Do
If Left(x, 6) = "111111" Then y = y - 6: x = Right(x, y): ans = ans & "9"
If Left(x, 6) = "111110" Then y = y - 6: x = Right(x, y): ans = ans & "8"
If Left(x, 6) = "111101" Then y = y - 6: x = Right(x, y): ans = ans & "7"
If Left(x, 6) = "111100" Then y = y - 6: x = Right(x, y): ans = ans & "6"
If Left(x, 5) = "11101" Then y = y - 5: x = Right(x, y): ans = ans & "5"
If Left(x, 5) = "11100" Then y = y - 5: x = Right(x, y): ans = ans & "4"
If Left(x, 4) = "1101" Then y = y - 4: x = Right(x, y): ans = ans & "3"
If Left(x, 4) = "1100" Then y = y - 4: x = Right(x, y): ans = ans & "2"
If Left(x, 3) = "101" Then y = y - 3: x = Right(x, y): ans = ans & "1"
If Left(x, 3) = "100" Then y = y - 3: x = Right(x, y): ans = ans & "0"
If Left(x, 2) = "00" Then y = y - 2: x = Right(x, y): ans = ans & "A"
If Left(x, 2) = "01" Then y = y - 2: x = Right(x, y): ans = ans & "B"
Loop Until y = 0
lon = Len(ans)
ans1 = Left(ans, 4)
ans2 = Right(ans, lon - 4)
Print #3, ans1 & "," & ans2
End Sub
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in1.txt" For Input As #1
Open App.Path & "\in2.txt" For Input As #2
Open App.Path & "\out.txt" For Output As #3
For z = 1 To 2
Input #z, x
For i = 1 To x
Line Input #z, y
p = Len(y)
t = ""
Do Until p = 0
If Left(y, 2) = "00" Then p = p - 2: y = Right(y, p): t = t & "A"
If Left(y, 2) = "01" Then p = p - 2: y = Right(y, p): t = t & "B"
If Left(y, 3) = "100" Then p = p - 3: y = Right(y, p): t = t & "0"
If Left(y, 3) = "101" Then p = p - 3: y = Right(y, p): t = t & "1"
If Left(y, 4) = "1100" Then p = p - 4: y = Right(y, p): t = t & "2"
If Left(y, 4) = "1101" Then p = p - 4: y = Right(y, p): t = t & "3"
If Left(y, 5) = "11100" Then p = p - 5: y = Right(y, p): t = t & "4"
If Left(y, 5) = "11101" Then p = p - 5: y = Right(y, p): t = t & "5"
If Left(y, 6) = "111100" Then p = p - 6: y = Right(y, p): t = t & "6"
If Left(y, 6) = "111101" Then p = p - 6: y = Right(y, p): t = t & "7"
If Left(y, 6) = "111110" Then p = p - 6: y = Right(y, p): t = t & "8"
If Left(y, 6) = "111111" Then p = p - 6: y = Right(y, p): t = t & "9"
Loop
t = Left(t, 4) & "," & Right(t, Len(t) - 4)
Print #3, t
Next
Print #3,
Next
Close
Close
Close
End
End Sub