2017年12月28日 星期四

To Z2A 程式選訓 (1220,1227)

1220,1227練習題及參考

1220撲克牌型

Dim card(4) As Integer '5張或分成5
Dim pt(4) As Integer '點數
Dim fs(4) As Integer '花色

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Randomize()
        Dim mk(51) As Boolean
        Dim cnt = 0
        Dim s As String = ""
        Do Until cnt = 5
            Dim x As Integer = Int(Rnd() * 52)
            If Not mk(x) Then
                mk(x) = True  '未出現過,可用
                card(cnt) = x
                cnt += 1
                s &= x & ","
            End If
        Loop
        Label1.Text = s
        Label1.Text &= "型別:" & chktyp()
    End Sub
 
  Function chktyp() As Integer ' card()的值 判斷牌型
        For i = 0 To 4
            fs(i) = card(i) \ 13        ' 0~ 3 花色
            pt(i) = card(i) Mod 13 + 1  '1~13  點數
        Next i
        chktyp = 0 '非有型則為 散牌 0
        Dim flus As Boolean = True '同花?
        For i = 1 To 4
            If fs(i) <> fs(i - 1) Then '與前一張不同花色
                flus = False
                Exit For
            End If
        Next i
        Dim pairs(4) As Integer '同點對數 統計
        Array.Sort(pt, 0, 5) '點數 CDN 張排序 pt(0:cdn-1)
        Dim cnt As Integer = 1 '自己張數 1
        For i = 1 To 4
            If pt(i) = pt(i - 1) Then '與前一張同點,cnt+1
                cnt += 1
            Else
                pairs(cnt) += 1
                cnt = 1
            End If
        Next
        pairs(cnt) += 1
        '    MsgBox("同點" & pairs(2) & pairs(3) & pairs(4))



        ' 是否順子
        Dim stra As Boolean = chkstra() '順子?
        If Not stra And pt(0) = 1 Then
            pt(0) = 14
            Array.Sort(pt, 0, 5) 'ace改為 14 chk一次
            stra = chkstra()
        End If
        If flus And stra Then
            chktyp = 8 '同花順
        ElseIf pairs(4) = 1 Then
            chktyp = 7 '四條
        ElseIf pairs(3) = 1 And pairs(2) = 1 Then
            chktyp = 6 ' 葫蘆
        ElseIf flus Then
            chktyp = 5 '同花
        ElseIf stra Then
            chktyp = 4 '
        ElseIf pairs(3) = 1 Then
            chktyp = 3 ' 三條
        ElseIf pairs(2) = 2 Then
            chktyp = 2 ' 兩對
        ElseIf pairs(2) = 1 Then
            chktyp = 1 ' 一對
        End If
        Return chktyp
    End Function
   
Function chkstra() As Boolean
        chkstra = True '順子?
        For i = 1 To 4
            If pt(i) - 1 <> pt(i - 1) Then '非前一張 點數+1
                Return False
            End If
        Next
    End Function

   

1227練習題目

[C_AR51-] 螺旋矩陣
問題描述
螺旋矩陣是一個短陣,其中每個 cell 都填滿整數,且每個整數以螺旋的方式排列。下圖為二個 size = 8 的螺旋矩陣。觀查圖 (a) ,數字由 1 開始,以逆時鐘的方向,螺旋狀地往中心排列。而圖 (b) 則是一個順時鐘的螺旋矩陣,和圖 (a) 不一樣之處在於其數字的旋轉方向為順時鐘旋轉。



請寫一個程式,接受輸入,並將螺旋矩陣印出。
輸入說明:
輸入為一行字串,包含了兩個數值,每個數值以逗點隔開。
第一個數值為 size ,其值介於 1 30 間( i.e., 1 <= size <= 30 )。
第二個數值為方向, 1 為順時鐘方向, 2 為逆時鐘方向。
輸出說明:
將螺旋矩陣輸出。每個數字的寬度為 3 ,不足寬度之處補 0 (也就是說,數字 1 要印輸出成 001 ,數字 12 要輸出成 012 …依此類推)。每個數字後要加一個逗點,數字間不要有空白。每一 row 的最後一個數字之後不要加逗點。
輸入範例:
7,1
輸出範例
001,002,003,004,005,006,007
024,025,026,027,028,029,008
023,040,041,042,043,030,009
022,039,048,049,044,031,010
021,038,047,046,045,032,011
020,037,036,035,034,033,012
019,018,017,016,015,014,013

撲克牌型,給5張牌(0~51),判斷其牌型印出
輸入範例:
0,9,10,11,12
2,15,41,28,1
4,2,1,0,3
2,15,41,4,30
2,6,7,4,3
1,29,43,0,15
3,29,16,15,1
34,35,1,21,14
20,7,8,9,10
15,7,9,10,8
輸出範例:
同花大順
四條
同花順
葫蘆
同花
順子
三條
兩對
一對
散牌

計算地雷數:踩地雷計算九宮地雷數
第1列輸入m , n b 代表有 m*n 個格子,有b個地雷,接著第2列有b個座標,左上角(0,0)列先
以下圖左為例,計算每一格四周九宮位位置有幾個地雷,地雷處不用算,計算結果如下圖右
   
      
輸入範例:
11,10,11
2,1 2,3 2,5 3,6 4,8 6,4 7,8 9,1 9,9, 10,6 10,9

輸出範例:
0 0 0 0 0 0 0 0 0 0
1 1 2 1 2 1 1 0 0 0
1 * 2 * 2 * 2 1 0 0
1 1 2 1 2 2 * 2 1 1
0 0 0 0 0 1 1 2 * 1
0 0 0 1 1 1 0 1 1 1
0 0 0 1 * 1 0 1 1 1
0 0 0 1 1 1 0 1 * 1
1 1 1 0 0 0 0 1 2 2
1 * 1 0 0 1 1 1 2 *
1 1 1 0 0 1 * 1 2 *

撲克21,給3張牌(0~51),判斷幾點,>21 爆,{A可當111JQK10點}
輸入範例:
0,2,6
13,10,11
26,42,8
11,25,30
輸出範例:
21
21
14
Bom


大數相加或大數相乘:兩個正整數a,b中間一個+*,算出其結果,1<=a,b<1050
輸入範例:
12345+98765432123456789
56*89
567+433
2147483648*7487812485248974848
輸出範例:
98765432123469134
4984
1000
16079954871362414694843285504



1227-1螺旋矩陣及1227-3數地雷

1227-1 螺蜁矩陣
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Hide()  :        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) ' in檔間 空一列
            Dim x As Integer = LineInput(fn) ' x 筆輸入,每筆兩個數字
            For k = 1 To x
                Dim s = LineInput(fn)  ' n,d 大小及方向
                PrintLine(3, fxx(s)) '每個輸出後空一列
            Next
        Next
    End Sub
    Function fxx(ByVal s As String) As String
        Dim dat() = s.Split(",")
        Dim N As Integer = dat(0), d As Integer = dat(1)
        Dim n2 As Integer = N * N   'N^2
        d -= 1 '1 ->0從右開始 ,逆2 ->1從下開始
        Dim m As Integer = 1 'd +1 +3
        If d = 1 Then m = 3
        fxx = ""
        Dim a(30, 30) As Integer
        '       d :0 1 2 3
        Dim dr() = {0, 1, 0, -1}
        Dim dc() = {1, 0, -1, 0}
        Dim r As Integer = 0, c As Integer = 0  ' (r,c)=(0,0) 左上開始
        Dim p = 1 '1開始填
        Do While (p <= n2)
            a(r, c) = p
            Dim nr = r + dr(d), nc = c + dc(d)
            If ((nr < 0 Or nr >= N Or nc < 0 Or nc >= N) OrElse a(nr, nc) <> 0) Then '轉向
                d = (d + m) Mod 4
                nr = r + dr(d) : nc = c + dc(d)
            End If
            r = nr : c = nc : p += 1
        Loop
        ' 輸出
        For r = 0 To N - 1
            fxx &= setw(3, "0", a(r, 0))
            For c = 1 To N - 1
                fxx &= "," & setw(3, "0", a(r, c))
            Next
            fxx &= vbNewLine
        Next
    End Function
    Function setw(ByVal w As Integer, ByVal fill As Char, ByVal v As Integer) As String
        setw = ""
        For i = 1 To w
            If v = 0 Then
                setw = fill & setw
            Else
                setw = (v Mod 10) & setw
                v \= 10
            End If
        Next
    End Function
1227-3 數地雷
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Hide()
        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) ' in檔間 空一列
            Dim x As Integer = LineInput(fn) ' x 筆輸入,每筆兩個數字
            For k = 1 To x
                Dim mnb() = LineInput(fn).Split(",")  ' m,n,b *行,雷數
                m = mnb(0) : n = mnb(1) : b = mnb(2)
                Dim s = LineInput(fn) '讀入座標
                PrintLine(3, fxx(s)) '每個輸出後空一列
            Next
        Next
        End
    End Sub
    'in1.txt 更正
    '11,10,11
    '2,1 2,3 2,5 3,6 4,8 6,4 7,8 9,1 9,9, 10,6 10,9
    Dim m, n, b As Integer
    Function fxx(ByVal s As String) As String
        Dim a(m + 1, n + 1) As Integer '外圍多一圈
        Dim dat() = s.Split(" ")
        Dim r, c As Integer
        For i = 0 To UBound(dat)  '讀地雷座標,設為 -1
            Dim rc() = dat(i).Split(",")
            r = rc(0) : c = rc(1)  '座標 (r,c)
            a(r + 1, c + 1) = -1 '改為從 (1,1)開始,往右下移1
        Next
        fxx = ""
        For r = 1 To m
            For c = 1 To n
                Dim cnt As Integer = 0 '四周九宮雷數
                If a(r, c) = -1 Then
                    fxx &= "* "
                    Continue For '這格是地雷,不用計算
                End If
                If a(r - 1, c - 1) = -1 Then cnt += 1 '左上
                If a(r - 1, c) = -1 Then cnt += 1 '
                If a(r - 1, c + 1) = -1 Then cnt += 1 '右上
                If a(r, c - 1) = -1 Then cnt += 1 '
                If a(r, c + 1) = -1 Then cnt += 1 '
                If a(r + 1, c - 1) = -1 Then cnt += 1 '左下
                If a(r + 1, c) = -1 Then cnt += 1 '
                If a(r + 1, c + 1) = -1 Then cnt += 1 '右下
                a(r, c) = cnt  '放入算出的個數
                fxx &= cnt & " "
            Next
            fxx &= vbNewLine
        Next
    End Function