2012年6月15日 星期五

陣列

1. 10人(座號1~10),3科成績,以總分排名次。
2. 10人(座號1~10),以總分排名次,同分者以座號小者優先。
3. 10 * 10的踩地雷,放入3顆炸彈,0~3顯示八方的炸彈顆數9為炸彈。
4. 10 * 10踩地雷中點X是0~9那個數字。
5. 數獨:a:9 * 9陣列判斷是否完成(正確)。
(數獨規則:在九個九宮格裡,填入1到9的數字,讓每個數字在每個行、列及九宮格裡都只出現一次。)
                b:列出9 * 9陣列位置1~9的所有可能。


12 則留言:

  1. 第一題:(同分者直接排同名)
    Private Sub Form_Load()
    Me.Hide
    Dim x, y, z, n(10, 1), d(10), t(10) As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    a = 0
    Do While Not EOF(1)
    a = a + 1
    Input #1, a, x, y, z
    b = x + y + z
    n(a, 1) = b
    d(a) = a
    Loop
    For j = 1 To 10
    For i = 1 To (10 - j)
    If n(i, 1) < n(i + 1, 1) Then
    c = n(i, 1)
    n(i, 1) = n(i + 1, 1)
    n(i + 1, 1) = c
    e = d(i)
    d(i) = d(i + 1)
    d(i + 1) = e
    End If
    Next
    Next
    For i = 1 To 10
    t(i) = 1
    For j = 1 To 10
    If n(i, 1) < n(j, 1) Then t(i) = t(i) + 1
    Next
    Print #2, "第"; t(i) & "名是" & d(i) & "號分數是" & n(i, 1)
    Next
    Close
    Close
    End
    End Sub

    第二題:(延續第一題,用三科成績來排序)
    Private Sub Form_Load()
    Me.Hide
    Dim x, y, z, n(10, 1), d(10), t(10) As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    a = 0
    Do While Not EOF(1)
    a = a + 1
    Input #1, a, x, y, z
    b = x + y + z
    n(a, 1) = b
    d(a) = a
    Loop
    For j = 1 To 10
    For i = 1 To (10 - j)
    If n(i, 1) < n(i + 1, 1) Then
    c = n(i, 1)
    n(i, 1) = n(i + 1, 1)
    n(i + 1, 1) = c
    e = d(i)
    d(i) = d(i + 1)
    d(i + 1) = e
    End If
    Next
    Next
    For i = 1 To 10
    Print #2, "第"; i & "名是" & d(i) & "號分數是" & n(i, 1)
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  2. 第一題
    Private Sub Form_Load()
    Me.Hide
    Randomize
    Open App.Path & "\out.txt" For Output As #1
    Dim s1(10), s2(10), s3(10), sum(10), m(10) As Integer
    For i = 1 To 10
    s1(i) = Int(Rnd() * 101)
    s2(i) = Int(Rnd() * 101)
    s3(i) = Int(Rnd() * 101)
    sum(i) = s1(i) + s2(i) + s3(i)
    m(i) = sum(i)
    Next i
    For j = 1 To 10
    For k = 1 To 9
    If m(k) < m(k + 1) Then
    b = m(k)
    m(k) = m(k + 1)
    m(k + 1) = b
    End If
    Next k
    Next j
    For a = 1 To 10
    For b = 1 To 10
    If sum(a) = m(b) Then
    Print #1, a, s1(a), s2(a), s3(a), m(b), b: Exit For
    End If
    Next b
    Next a
    Close
    End
    End Sub
    ------------------------------------------------------------
    第二題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim sum(10), x(10) As Integer
    f = 1
    Do
    Input #1, sum(f)
    x(f) = f
    f = f + 1
    Loop Until EOF(1)
    For i = 1 To 10
    For j = 1 To 9
    If sum(j) < sum(j + 1) Then
    c = sum(j)
    sum(j) = sum(j + 1)
    sum(j + 1) = c
    e = x(j)
    x(j) = x(j + 1)
    x(j + 1) = e
    End If
    Next j
    Next i
    For g = 1 To 10
    For k = 1 To 10
    If g = x(k) Then
    Print #2, x(k), sum(k), k
    End If
    Next k
    Next g
    Close #2
    Close #1
    End
    End Sub
    ---------------------------------------------------------------
    第三題
    Private Sub Form_Load()
    Me.Hide
    Randomize Timer
    Open App.Path & "\out.txt" For Output As #1
    Dim bt(11, 11) As Integer
    For c = 1 To 3
    a = Int(Rnd() * 10) + 1
    b = Int(Rnd() * 10) + 1
    bt(a, b) = 9
    Next c
    For i = 1 To 10
    For j = 1 To 10
    If bt(i, j) <> 9 Then
    x = 0
    If bt(i, j - 1) = 9 Then x = x + 1
    If bt(i - 1, j - 1) = 9 Then x = x + 1
    If bt(i - 1, j) = 9 Then x = x + 1

    If bt(i, j + 1) = 9 Then x = x + 1
    If bt(i + 1, j + 1) = 9 Then x = x + 1
    If bt(i + 1, j) = 9 Then x = x + 1
    If bt(i - 1, j + 1) = 9 Then x = x + 1
    If bt(i + 1, j - 1) = 9 Then x = x + 1
    bt(i, j) = x
    End If
    Print #1, bt(i, j);
    Next j
    Print #1,
    Next i
    Close #1
    End
    End Sub

    回覆刪除
  3. 第三題:
    Private Sub Form_Load()
    Randomize
    Me.Hide
    Dim x(11, 11)
    Open App.Path & "\out.txt" For Output As #1
    For j = 1 To 10
    For i = 1 To 10
    x(j, i) = 0
    Next
    Next
    For i = 1 To 3
    a = Int(Rnd() * 10) + 1
    b = Int(Rnd() * 10) + 1
    x(a, b) = 9
    Next

    For j = 1 To 10
    For i = 1 To 10
    If x(j, i) = 9 Then
    Else
    If x(j - 1, i - 1) = 9 Then x(j, i) = x(j, i) + 1
    If x(j - 1, i) = 9 Then x(j, i) = x(j, i) + 1
    If x(j - 1, i + 1) = 9 Then x(j, i) = x(j, i) + 1
    If x(j, i - 1) = 9 Then x(j, i) = x(j, i) + 1
    If x(j, i) = 9 Then x(j, i) = x(j, i) + 1
    If x(j, i + 1) = 9 Then x(j, i) = x(j, i) + 1
    If x(j + 1, i - 1) = 9 Then x(j, i) = x(j, i) + 1
    If x(j + 1, i) = 9 Then x(j, i) = x(j, i) + 1
    If x(j + 1, i + 1) = 9 Then x(j, i) = x(j, i) + 1
    End If
    Next
    Next
    For j = 1 To 10
    For i = 1 To 10
    Print #1, x(j, i);
    Next
    Print #1,
    Next
    Close
    End
    End Sub

    第四題:
    不太明白題目意思!
    第五題:
    寫的程式少了一個規則,所以錯了還要改。

    回覆刪除
  4. 第五題:(A)
    Private Sub Form_Load()
    Me.Hide
    Randomize
    Dim r As Boolean
    Dim x(9, 9), y(9, 9) As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    For i = 1 To 9
    For j = 1 To 9
    Input #1, n
    g = Int(Rnd() * 9) + 1
    x(i, j) = n '讀檔、亂數都可以。目前是讀檔
    Next
    Next
    For i = 1 To 9
    For j = 1 To 9
    a = x(i, j)
    b = x(j, i)
    For k = 1 To 9
    If a = x(i, k) And a <> 0 Then y(i, j) = y(i, j) + 1
    If b = x(k, i) And b <> 0 Then y(i, j) = y(i, j) + 1
    Next
    Next
    Next
    For i = 1 To 9
    For j = 1 To 9
    If i Mod 3 = 2 And j Mod 3 = 2 Then
    For i1 = 1 To 9
    If x(j - 1, i - 1) = i1 Then y(j - 1, i - 1) = y(j - 1, i - 1) + 1
    If x(j - 1, i) = i1 Then y(j - 1, i) = y(j - 1, i) + 1
    If x(j - 1, i + 1) = i1 Then y(j - 1, i + 1) = y(j - 1, i + 1) + 1
    If x(j, i - 1) = i1 Then y(j, i - 1) = y(j, i - 1) + 1
    If x(j, i) = i1 Then y(j, i) = y(j, i) + 1
    If x(j, i + 1) = i1 Then y(j, i + 1) = y(j, i + 1) + 1
    If x(j + 1, i - 1) = i1 Then y(j + 1, i - 1) = y(j + 1, i - 1) + 1
    If x(j + 1, i) = i1 Then y(j + 1, i) = y(j + 1, i) + 1
    If x(j + 1, i + 1) = i1 Then y(j + 1, i + 1) = y(j + 1, i + 1) + 1
    Next
    End If
    Next
    Next
    r = True
    For i = 1 To 9
    For j = 1 To 9
    If y(i, j) <> 3 Then r = False
    Next
    Next
    If r = False Then
    Print #2, "未完成,不正確"
    Else
    Print #2, "完成,正確"
    End If
    Close
    Close
    End
    End Sub

    回覆刪除
  5. 第一題

    Private Sub Form_Load()
    Randomize Timer
    Open App.Path & "\in.txt" For Output As #1
    Dim a(10), b(10), c(10), x(10) As Integer
    Dim n(10), h As String
    For i = 1 To 10
    a(i) = Int(Rnd() * 100) + 1
    b(i) = Int(Rnd() * 100) + 1
    c(i) = Int(Rnd() * 100) + 1
    x(i) = a(i) + b(i) + c(i)
    n(i) = i & "號 " & a(i) & " " & b(i) & " " & c(i)
    Print #1, n(i)
    Next i
    Close

    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim num(10), ad(10), st(10)
    For i = 1 To 10
    Line Input #1, num(i)
    g = Split(num(i), " ")
    ad(i) = Val(g(2)) + Val(g(3)) + Val(g(4))
    st(i) = i & "號 " & g(2) & " " & g(3) & " " & g(4) & " 共:" & ad(i)
    Next i

    For i = 1 To 9
    For j = 1 To 9
    If ad(j) < ad(j + 1) Then
    sco = ad(j)
    ad(j) = ad(j + 1)
    ad(j + 1) = sco
    r = st(j)
    st(j) = st(j + 1)
    st(j + 1) = r
    End If
    Next j
    Next i

    For i = 1 To 10
    Print #2, st(i) & "分 第" & i & "名"
    Next i
    Close
    Close
    End Sub
    -------------------------------------------
    第二題

    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim num(10), ad(10), st(10)
    For i = 1 To 10
    Line Input #1, num(i)
    g = Split(num(i), " ")
    ad(i) = Val(g(2)) + Val(g(3)) + Val(g(4))
    st(i) = i & "號 " & ad(i) & "分"
    Print st(i)
    Next i

    For i = 1 To 9
    For j = 1 To 9
    If ad(j) < ad(j + 1) Then
    sco = ad(j)
    ad(j) = ad(j + 1)
    ad(j + 1) = sco
    r = st(j)
    st(j) = st(j + 1)
    st(j + 1) = r
    End If
    Next j
    Next i

    For i = 1 To 10
    Print #2, st(i) & i & "名"
    Next i
    Close
    Close
    ------------------------------------------
    第三題

    Private Sub Form_Load()
    Randomize Timer
    Dim n(11, 11)
    For i = 1 To 10
    For j = 1 To 10
    n(j, i) = 0
    Next j
    Next i
    For i = 1 To 3
    a = Int(Rnd() * 10) + 1
    b = Int(Rnd() * 10) + 1
    n(a, b) = 9
    Next i

    For i = 1 To 10
    For j = 1 To 10
    If n(j, i) = 9 Then
    Else
    If n(j - 1, i - 1) = 9 Then n(j, i) = n(j, i) + 1
    If n(j - 1, i) = 9 Then n(j, i) = n(j, i) + 1
    If n(j - 1, i + 1) = 9 Then n(j, i) = n(j, i) + 1
    If n(j, i - 1) = 9 Then n(j, i) = n(j, i) + 1
    If n(j, i + 1) = 9 Then n(j, i) = n(j, i) + 1
    If n(j + 1, i - 1) = 9 Then n(j, i) = n(j, i) + 1
    If n(j + 1, i) = 9 Then n(j, i) = n(j, i) + 1
    If n(j + 1, i + 1) = 9 Then n(j, i) = n(j, i) + 1
    End If
    Next j
    Next i

    For y = 1 To 10
    For x = 1 To 10
    Print n(x, y);
    Next x
    Print
    Next y
    End Sub

    回覆刪除
  6. 5A:
    概念是直行、橫行、9個九宮格有沒有等於1~9的總和
    Private Sub Form_Load()
    Me.Hide
    Randomize
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim m(9, 9) As Integer
    Dim t As Boolean
    For i = 1 To 9
    For j = 1 To 9
    Input #1, m(i, j)
    Next j
    Next i
    For g = 1 To 9
    sum1 = sum1 + g
    Next g
    For a = 1 To 9
    s1 = 0: s2 = 0
    For b = 1 To 9
    s1 = s1 + m(a, b)
    s2 = s2 + m(b, a)
    If sum1 = s1 Then t = True Else t = False
    If sum1 = s2 Then t = True Else t = False
    Next b
    Next a
    For c = 1 To 9
    For d = 1 To 9
    If c Mod 3 = 2 And d Mod 3 = 2 Then
    sum2 = 0
    For e = 1 To 9
    If m(d - 1, c - 1) = e Then sum2 = sum2 + e
    If m(d - 1, c) = e Then sum2 = sum2 + e
    If m(d - 1, c + 1) = e Then sum2 = sum2 + e
    If m(d, c - 1) = e Then sum2 = sum2 + e
    If m(d, c) = e Then sum2 = sum2 + e
    If m(d, c + 1) = e Then sum2 = sum2 + e
    If m(d + 1, c - 1) = e Then sum2 = sum2 + e
    If m(d + 1, c) = e Then sum2 = sum2 + e
    If m(d + 1, c + 1) = e Then sum2 = sum2 + e
    Next e
    End If
    If sum1 = sum2 Then t = True Else t = False
    Next d
    Next c
    Print #2, t
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  7. 第五題:(A) 前次迴圈內判斷錯誤
    Private Sub Form_Load()
    Me.Hide
    Randomize
    Dim r As Boolean
    Dim x(9, 9), y(9, 9) As Integer
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    For i = 1 To 9
    For j = 1 To 9
    Input #1, n
    g = Int(Rnd() * 9) + 1
    x(i, j) = n '讀檔、亂數都可以。目前是讀檔
    Next
    Next
    For i = 1 To 9
    For j = 1 To 9
    a = x(i, j)
    b = x(j, i)
    For k = 1 To 9
    If a = x(i, k) And a <> 0 Then y(i, j) = y(i, j) + 1
    If b = x(k, i) And b <> 0 Then y(i, j) = y(i, j) + 1
    Next
    Next
    Next
    r = True
    For j = 2 To 8 Step 3
    For i = 2 To 8 Step 3
    For i1 = 1 To 9
    If x(j - 1, i - 1) = i1 Then y(j - 1, i - 1) = y(j - 1, i - 1) + 1: o = o + 1
    If x(j - 1, i) = i1 Then y(j - 1, i) = y(j - 1, i) + 1: o = o + 1
    If x(j - 1, i + 1) = i1 Then y(j - 1, i + 1) = y(j - 1, i + 1) + 1: o = o + 1
    If x(j, i - 1) = i1 Then y(j, i - 1) = y(j, i - 1) + 1: o = o + 1
    If x(j, i) = i1 Then y(j, i) = y(j, i) + 1: o = o + 1
    If x(j, i + 1) = i1 Then y(j, i + 1) = y(j, i + 1) + 1: o = o + 1
    If x(j + 1, i - 1) = i1 Then y(j + 1, i - 1) = y(j + 1, i - 1) + 1: o = o + 1
    If x(j + 1, i) = i1 Then y(j + 1, i) = y(j + 1, i) + 1: o = o + 1
    If x(j + 1, i + 1) = i1 Then y(j + 1, i + 1) = y(j + 1, i + 1) + 1: o = o + 1
    If o >= 2 Then r = False: Print #2, "未完成,不正確": End
    o = 0
    Next
    Next
    Next

    For i = 1 To 9
    For j = 1 To 9
    If y(j, i) <> 3 Then r = False

    Next
    Next

    If r = False Then
    Print #2, "未完成,不正確"
    Else
    Print #2, "完成,正確"
    End If
    Close
    Close
    End
    End Sub

    回覆刪除
  8. 5-a

    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim n(9, 9), g
    Dim x As Boolean
    x = True
    For i = 1 To 9
    For j = 1 To 9
    Input #1, n(i, j)
    Next j
    Next i

    For i = 1 To 9
    g = 0
    For j = 1 To 9
    g = g + n(i, j)
    Next j
    If g <> 45 Then x = False
    Next i

    For j = 1 To 9
    g = 0
    For i = 1 To 9
    g = g + n(i, j)
    Next i
    If g <> 45 Then x = False
    Next j

    For i = 2 To 8 Step 3
    For j = 2 To 8 Step 3
    g = n(i, j)
    g = g + n(i - 1, j - 1) + n(i - 1, j) + n(i - 1, j + 1) + n(i, j - 1) + n(i, j + 1) + n(i + 1, j - 1) + n(i + 1, j) + n(i + 1, j + 1)
    If g <> 45 Then x = False
    Next j
    Next i
    Print x
    Close
    Close
    End Sub

    回覆刪除
  9. 第三題:多了一個X值
    Private Sub Form_Load()
    Randomize
    Me.Hide
    Dim x(11, 11)
    Open App.Path & "\out.txt" For Output As #1
    For j = 1 To 10
    For i = 1 To 10
    x(j, i) = 0
    Next
    Next
    Do
    a = Int(Rnd() * 10) + 1
    b = Int(Rnd() * 10) + 1
    If x(a, b) <> 9 Then
    x(a, b) = 9
    c = c + 1
    End If
    Loop Until c = 3

    For j = 1 To 10
    For i = 1 To 10
    If x(j, i) = 9 Then
    Else
    If x(j - 1, i - 1) = 9 Then x(j, i) = x(j, i) + 1
    If x(j - 1, i) = 9 Then x(j, i) = x(j, i) + 1
    If x(j - 1, i + 1) = 9 Then x(j, i) = x(j, i) + 1
    If x(j, i - 1) = 9 Then x(j, i) = x(j, i) + 1
    If x(j, i) = 9 Then x(j, i) = x(j, i) + 1
    If x(j, i + 1) = 9 Then x(j, i) = x(j, i) + 1
    If x(j + 1, i - 1) = 9 Then x(j, i) = x(j, i) + 1
    If x(j + 1, i) = 9 Then x(j, i) = x(j, i) + 1
    If x(j + 1, i + 1) = 9 Then x(j, i) = x(j, i) + 1
    End If
    Next
    Next
    a = Int(Rnd() * 10 + 1)
    b = Int(Rnd() * 10 + 1)
    x(a, b) = "x"
    For j = 1 To 10
    For i = 1 To 10
    Print #1, x(j, i) & " ";
    Next
    Print #1,
    Next
    Close
    End
    End Sub

    第四題:解X值
    Private Sub Form_Load()
    Me.Hide
    Dim x(11, 11) As String
    Dim y(11, 11) As Integer
    Dim t As Integer
    Dim f As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    For i = 1 To 10
    Line Input #1, n
    a = Split(n)
    For j = 0 To UBound(a)
    x(i, j + 1) = a(j)
    Next
    Next
    c = 0
    For i = 1 To 10
    For j = 1 To 10
    If x(i, j) = "x" Then f = i: t = j
    If x(i, j) = "9" Then c = c + 1
    Print #2, x(i, j) & " ";
    Next
    Print #2,
    Next

    If x(f, t + 1) = "9" Then y(f, t) = y(f, t) + 1
    If x(f, t - 1) = "9" Then y(f, t) = y(f, t) + 1
    If x(f + 1, t) = "9" Then y(f, t) = y(f, t) + 1
    If x(f + 1, t - 1) = "9" Then y(f, t) = y(f, t) + 1
    If x(f + 1, t + 1) = "9" Then y(f, t) = y(f, t) + 1
    If x(f - 1, t) = "9" Then y(f, t) = y(f, t) + 1
    If x(f - 1, t - 1) = "9" Then y(f, t) = y(f, t) + 1
    If x(f - 1, t + 1) = "9" Then y(f, t) = y(f, t) + 1
    x(f, t) = y(f, t)
    If c <> 3 Then x(f, t) = 9
    Print #2,
    For i = 1 To 10
    For j = 1 To 10
    Print #2, x(i, j) & " ";
    Next
    Print #2,
    Next
    Print #2,
    Print #2, "x(" & f & "," & t & ")= ";
    Print #2, x(f, t)
    Close
    Close
    End
    End Sub

    回覆刪除
  10. 第五題:(B)只能解一個未知數(利用直排橫排1~9數字各一個的方式下去解)
    Private Sub Form_Load()
    Me.Hide
    Randomize
    Dim x(9, 9) As Integer
    Dim y(9, 9) As Integer
    Dim f, t As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Print #2, "解答前的數獨:"
    For i = 1 To 9
    For j = 1 To 9
    Input #1, n
    x(i, j) = n
    Print #2, x(i, j);
    Next
    Print #2,
    Next
    For i = 1 To 9
    For j = 1 To 9
    a = x(i, j)
    b = x(j, i)
    For k = 1 To 9
    If a = x(i, k) And a <> 0 Then y(i, j) = y(i, j) + 1
    If b = x(k, i) And b <> 0 Then y(i, j) = y(i, j) + 1
    Next
    Next
    Next
    r = True
    For i = 1 To 9
    For j = 1 To 9
    If y(i, j) <> 2 Then
    r = False
    t = i
    f = j
    Exit For
    End If
    Next
    Next

    If r = False Then
    For i = 1 To 9
    x(f, t) = i
    For j = 1 To 9
    For k = 1 To 9
    If x(f, t) = x(k, t) And x(f, t) <> 0 Then y(f, t) = y(f, t) + 1
    If x(f, t) = x(f, k) And x(f, t) <> 0 Then y(f, t) = y(f, t) + 1
    Next

    If y(f, t) = 2 Then

    Print #2, "解答後的數獨:"
    For i2 = 1 To 9
    For j2 = 1 To 9
    Print #2, x(i2, j2);
    Next
    Print #2,
    Next
    Print #2,
    End
    Else
    y(f, t) = 0
    End If
    Next
    Next
    End If
    Close
    Close
    End
    End Sub

    回覆刪除
  11. 5-b
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim n(9, 9), g
    For i = 1 To 9
    For j = 1 To 9
    Input #1, n(j, i)
    Next j
    Next i
    Print #2, "解題前:"
    For i = 1 To 9
    For j = 1 To 9
    Print #2, n(j, i) & " ";
    Next j
    Print #2,
    Next i


    For i = 1 To 9
    For j = 1 To 9
    If n(j, i) = 0 Then
    Select Case (j - 1) Mod 3
    Case Is = 0
    Select Case (i - 1) Mod 3
    Case Is = 0
    g = 45 - (n(j, i + 1) + n(j, i + 2) + n(j + 1, i) + n(j + 1, i + 1) + n(j + 1, i + 2) + n(j + 2, i) + n(j + 2, i + 1) + n(j + 2, i + 2))
    Case Is = 1
    g = 45 - (n(j, i + 1) + n(j, i - 1) + n(j + 1, i) + n(j + 1, i + 1) + n(j + 1, i - 1) + n(j + 2, i) + n(j + 2, i + 1) + n(j + 2, i - 1))
    Case Is = 2
    g = 45 - (n(j, i - 1) + n(j, i - 2) + n(j + 1, i) + n(j + 1, i - 1) + n(j + 1, i - 2) + n(j + 2, i) + n(j + 2, i - 1) + n(j + 2, i - 2))
    End Select
    Case Is = 1
    Select Case (i - 1) Mod 3
    Case Is = 0
    g = 45 - (n(j - 1, i) + n(j - 1, i + 1) + n(j - 1, i + 2) + n(j, i + 1) + n(j, i + 2) + n(j + 1, i) + n(j + 1, i + 1) + n(j + 1, i + 2))
    Case Is = 1
    g = 45 - (n(j - 1, i - 1) + n(j - 1, i) + n(j - 1, i + 1) + n(j, i - 1) + n(j, i + 1) + n(j + 1, i - 1) + n(j + 1, i) + n(j + 1, i + 1))
    Case Is = 2
    g = 45 - (n(j - 1, i) + n(j - 1, i - 1) + n(j - 1, i - 2) + n(j, i - 1) + n(j, i - 2) + n(j + 1, i) + n(j + 1, i - 1) + n(j + 1, i - 2))
    End Select
    Case Is = 2
    Select Case (i - 1) Mod 3
    Case Is = 0
    g = 45 - (n(j - 2, i + 1) + n(j - 2, i + 2) + n(j - 2, i) + n(j - 1, i + 1) + n(j - 1, i + 2) + n(j - 1, i) + n(j, i + 1) + n(j, i + 2))
    Case Is = 1
    g = 45 - (n(j - 2, i + 1) + n(j - 2, i - 1) + n(j - 2, i) + n(j - 1, i + 1) + n(j - 1, i - 1) + n(j - 1, i) + n(j, i + 1) + n(j, i - 1))
    Case Is = 2
    g = 45 - (n(j - 2, i - 2) + n(j - 2, i - 1) + n(j - 2, i) + n(j - 1, i - 2) + n(j - 1, i - 1) + n(j - 1, i) + n(j, i - 1) + n(j, i - 2))
    End Select
    End Select
    End If
    Next j
    Next i

    Print #2, "-----------------"
    Print #2, "解題後:"
    For i = 1 To 9
    For j = 1 To 9
    If n(j, i) = 0 Then n(j, i) = g
    Print #2, n(j, i) & " ";
    Next j
    Print #2,
    Next i

    Close
    Close
    End
    End Sub

    回覆刪除
  12. 第五題(B):上一個寫法是錯誤的,這次這個可以解九宮格裡一個未知數的解,是利用九宮格裡1~9個數字不重複的特性解。
    Private Sub Form_Load()
    Me.Hide
    Dim x(10, 10) As Integer
    Dim y(9) As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    For i = 1 To 9
    For j = 1 To 9
    Input #1, n
    x(i, j) = n
    Print #2, n;
    Next
    Print #2,
    Next
    Print #2,

    For i = 2 To 8 Step 3
    For j = 2 To 8 Step 3
    f = 0: t = 0
    If x(i, j) = 0 Then f = i: t = j
    If x(i, j - 1) = 0 Then f = i: t = (j - 1)
    If x(i, j + 1) = 0 Then f = i: t = (j + 1)
    If x(i + 1, j) = 0 Then f = (i + 1): t = j
    If x(i + 1, j - 1) = 0 Then f = (i + 1): t = (j - 1)
    If x(i + 1, j + 1) = 0 Then f = (i + 1): t = (j + 1)
    If x(i - 1, j) = 0 Then f = (i - 1): t = j
    If x(i - 1, j - 1) = 0 Then f = (i - 1): t = (j - 1)
    If x(i - 1, j + 1) = 0 Then f = (i - 1): t = (j + 1)
    If f <> 0 And t <> 0 Then
    For k = 1 To 9
    If x(i, j) = k Then y(k) = y(k) + 1
    If x(i, j - 1) = k Then y(k) = y(k) + 1
    If x(i, j + 1) = k Then y(k) = y(k) + 1
    If x(i + 1, j) = k Then y(k) = y(k) + 1
    If x(i + 1, j - 1) = k Then y(k) = y(k) + 1
    If x(i + 1, j + 1) = k Then y(k) = y(k) + 1
    If x(i - 1, j) = k Then y(k) = y(k) + 1
    If x(i - 1, j - 1) = k Then y(k) = y(k) + 1
    If x(i - 1, j + 1) = k Then y(k) = y(k) + 1
    If y(k) = 0 Then x(f, t) = k
    Next
    End If
    For k1 = 1 To 9
    y(k1) = 0
    Next
    Next
    Next

    For i = 1 To 9
    For j = 1 To 9
    Print #2, x(i, j);
    Next
    Print #2,
    Next
    Close
    Close
    End
    End Sub

    回覆刪除