101年正式題第3題-子題2-編碼
題目大意:讀入一串只有0、1的字串,由下列編碼表找出對應的字元
二進制
|
字元
|
|
二進制
|
字元
|
00
|
A
|
11100
|
4
|
|
01
|
B
|
11101
|
5
|
|
100
|
0
|
111100
|
6
|
|
101
|
1
|
111101
|
7
|
|
1100
|
2
|
111110
|
8
|
|
1101
|
3
|
111111
|
9
|
輸入檔(in.txt)
6
1111001111011111101111111000010001
101110011101110111000010101
11100110111001011110001
111011100110111100110100
1011100100111111110000
1011100110111100110001
|
輸出檔(ou.txt)
6789,0A0B
1253,2A1B
4321,4B
5234,3A
1209,2A
1234,2B
|
解題參考說明:
讀入的字串由第1個位元開始,
b若為1 {判斷第5個連續1 (1)→轉成字元}
b若為1 {判斷第5個連續1 (1)→轉成字元}
b若不為1 (2) →轉成字元
轉成字元:假設第j個位元是連續5個1或第j個字元是0
( j之前連續1的個數one 、第j+1位元為d)
換算碼 k = 2*one+d+1 由右字串找 tbl= "AB0123456789"
one的個數要歸零
程式碼如下:
Dim tbl = "AB0123456789"
FileOpen(1, "in.txt",
OpenMode.Input)
FileOpen(3, "ou.txt",
OpenMode.Output)
Dim fn As Integer = 1
Dim n As Short = LineInput(fn)
For i = 1 To n
Dim line As String = LineInput(fn)
Dim one As Short = 0 '位元連續1的個數
Dim dcnt As Short = 0
Dim k As Integer
For j = 1 To line.Length
Dim b As Char = Mid(line, j,
1) '讀1個位元
If b = "1" Then
one += 1
If one = 5 Then
j += 1 ' (1)第5個1直接 以後1位元判斷是 8 或 9
k = 2 * one + 1 + Mid(line,
j, 1)
If dcnt = 4 Then Print(3,
",") '已印4個數字:加 , 號 才接 「aAbB」
Print(3, Mid(tbl, k, 1))
dcnt += 1 '印1個數字
one = 0
End If
Else ' (2) 讀入的是 0 ,一次處理後2位
j += 1 '以左邊 1 的個數 及 右邊0或1判斷
k = 2 * one + 1 + Mid(line, j,
1)
If dcnt = 4 Then Print(3,
",") '已印4個數字:加 , 號 才接 「aAbB」
Print(3, Mid(tbl, k, 1))
dcnt += 1
one = 0
End If
Next j
PrintLine(3)
Next
End