2016年10月21日 星期五

Z2A練習:M3P12

M3P12 103P12 解二元一次方程式
參考程式碼
   Function M3p12(ByVal s As String) As String
        Dim dt() = s.Split(",")  '讀一列依 「,」分成6個字串
        Dim a As Integer = dt(0), b As Integer = dt(1)
        Dim c As Integer = dt(2), d As Integer = dt(3)
        Dim e As Integer = dt(4), f As Integer = dt(5)
        'rem 聯立式(1)ax+by=c  (1)*e - (2)*b (ae-db)x = (ce-fb)
        'rem 聯立式(2)dx+ey=f  (1)*d - (2)*a (bd-ea)y = (cd-fa)
        Dim x As Integer = (c * e - f * b) / (a * e - d * b)
        Dim y As Integer = (c * d - f * a) / (b * d - e * a)
        Return x & "," & y
    End Function
----------------------
in1.txt
----------------------
2
0,2,20,1,0,10
9,5,17,33,6,-24
----------------------
in2.txt
----------------------
2
2,5,2,1,2,7
2,5,95,-10,2,-16
----------------------
out.txt
----------------------
10,10
-2,7

31,-12
5,17


Z2A練習:M2P11

M2P11 102P11 字串中的阿拉伯數字
參考程式碼:
   Function M2P11(ByVal s As String) As String
        Dim num = ""
        For i = 1 To s.Length
            Dim c As Char = Mid(s, i, 1)
            If (c >= "0" And c <= "9") Then num &= c
        Next
        If num.Length = 0 Then Return "N"
        Return num
    End Function
----------------------
in1.txt
----------------------
2
09ab2cckd7
ab21cdgcbgq123
----------------------
in2.txt
----------------------
3
129vbvs213zxcv7
Nb22429hhh
NB
----------------------
out.txt
----------------------
0927
21123

1292137
22429

N

Z2A練習:M5P02-smp2-學號

(2)類似題舉例2 資商學院的學號有5位,第1碼為系碼,第2碼為年度,第3碼為班碼,後2碼為座號
 系碼只能是SYZ其中之1,年度只能是2~5其中之一,班碼只能是1~3,座號一定是兩位01~50
 讀入n列字串,判斷是否為正確的學號,正確則則輸出R,不正確則輸出E

解題提示:系碼可設定一字串 hstr=”SYZ”,可以instr(hstr,c)函數尋找也可以用IF判斷
參考程式碼:
Dim hstr As String =”SYZ”   'Instr用
Function m5p02(ByVal s As String) As String
        m5p02 = "R"
        s = Trim(s)             '去掉首尾的空白
        If s.Length <> 5 Then Return "E"           '長度不對
        Dim c As Char = Mid(s, 1, 1)      '第1碼只能 SYZ之一
        If c <> "S" And c <> "Y" And c <> "Z" Then Return "E"
      '  If instr(hstr,c)=0 then return E”          '也可以改用 Instr找不到傳回0
        c = Mid(s, 2, 1)
        If c < "2" Or c > "5" Then Return "E"            '第2碼只能 2~5
        c = Mid(s, 3, 1)
        If c < "1" Or c > "3" Then Return "E"          '第3碼只能 1~3
        c = Mid(s, 4, 1)
        If c < "0" Or c > "5" Then Return "E"          '第4碼只能 0~5
        c = Mid(s, 5, 1)
        If c < "0" Or c > "9" Then Return "E"          '第5碼只能 0~9
        Dim no As Integer = Val(Mid(s, 4, 5))           ' 4、5兩碼合成一個數 只能 1~50
        If no < 1 Or no > 50 Then Return "E"
    End Function
-----------------------------------------
in1.txt
-----------------------------------------
3
Z1345
Y3150
S4234
-----------------------------------------
in2.txt
-----------------------------------------
5
X5123
Z53450
Y3151
S234A
Z2209
-----------------------------------------
out.txt
-----------------------------------------
E
R
R

E
E
E
E
R

Z2A練習:M5P01-smp1-11倍數

(1)類似題舉例1:判斷正整數0<N<1050是否可被 11 整除(因數字超過 Long型別)需使用字串處理
202914184810805067776
8239728901483491109728570834944

解題提示:奇數位的和 偶數位的和 是否為 11的倍數,是則輸出Y,不是則輸出N

參考程式碼如下:
   Function M5P01(ByVal s As String) As String
        Dim len As Integer = s.Length
        Dim odd As Integer = 0   '奇數位和
        Dim even As Integer = 0  '偶數位和
        For i = 1 To len
            If i Mod 2 = 0 Then
                even += Val(Mid(s, i, 1))
            Else
                odd += Val(Mid(s, i, 1))
            End If
        Next
        If Math.Abs(odd - even) Mod 11 = 0 Then
            Return "Y"
        Else
            Return "N"
        End If
    End Function
-----------------------------------------
in1.txt
-----------------------------------------
2
202914184810805067776
8239728901483491109728570834944
-----------------------------------------
in2.txt
-----------------------------------------
3
285311670611
14300
99999999999999999999999999968877
-----------------------------------------
out.txt
-----------------------------------------
Y
N

Y
Y
N

Z2A練習:讀檔架構

各題之讀檔架構省略,只提供一次如下
 FileOpen(1, "in1.txt", OpenMode.Input)
 FileOpen(2, "in2.txt", OpenMode.Input)
 FileOpen(3, "out.txt", OpenMode.Output)
 For fn = 1 To 2
     If fn = 2 Then PrintLine(3)
     Dim n As Integer = LineInput(fn)
     For i = 1 To n
       Dim line As String = LineInput(fn)
       PrintLine(3, MyPxx(line))  '呼叫 MyPxx 傳入一列字串,傳回規定解答
     Next
 Next

 End

Z2A練習:M3P11

M3P11 103P11 判斷是否為質數



Function M3P11(ByVal S As String) As String
        Dim K As Integer = Val(S)
        If K < 2 Then Return "N"
        For I = 2 To Math.Sqrt(K)
            If K Mod I = 0 Then Return "N"
        Next
        Return "Y"

  End Function

Z2A練習:M1P31

M1P31 101P31 身份證檢查

    Dim tno() = {10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21, 22, 35, 23, 24, 25, 26, 27, 28, 29, 32, 30, 31, 33}
               ‘  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z
    Function M1P31(ByVal pid As String) As String
        If (pid.Length <> 10) Then Return "F" ' 10 個字元
        Dim c As Char = Mid(pid, 1, 1)
        ' 是否為英文字母
        If c < "A" Or c > "Z" Then Return "F" '1個字為大寫字母
        Dim num As Integer = tno(Asc(c) - 65) '-Asc("A")  'A=0 tno()陣列找對應的
        Dim SUM As Integer = (num \ 10) * 1 + (num Mod 10) * 9
        c = Mid(pid, 2, 1)
        ' 是否為 1 2
        If c < "1" Or c > "2" Then Return "F" '2個字為 1 2
        SUM += (Asc(c) - 48) * 8
        For i = 3 To 9                     '3~9需數字
            c = Mid(pid, i, 1)
            If c < "0" Or c > "9" Then Return "F"
            SUM += (Asc(c) - 48) * (10 - i)  '分別乘 7 ~ 1
        Next
        c = Mid(pid, 10, 1)    '最後一個是檢查碼 ,再加上去可被10整除才對
        If c < "0" Or c > "9" Then Return "F"
        SUM += (Asc(c) - 48)
        If SUM Mod 10 = 0 Then Return "T" Else Return "F"
    End Function
----------------------
Integer 整數型別
String 字串型別
Char 字元型別
ByVal 傳值參數{主程式與副程式共用}
ByRef 傳址參數{主程式與副程式 共用}
字串.Length :字串的長度(字元數)
Mid(字串, m, n) 字串的第m個字起取 n
字元 A~ZASCII 65~90
  “0~9ASCII 48~57
Asc(字串) 字串的第1個字的Ascii

 
in1.txt
----------------------
3
M123456789
Y123456788
A223344556
----------------------
in2.txt
----------------------
3
R102345678
A108881111
B101111111
----------------------
out.txt
----------------------
T
T
F

T
F

T