2012年10月15日 星期一

VB101程式小考

VB101程式小考


1.若一個魔方陣 方格內的數均為質數,則稱此為質數魔方陣。
 已知在小於120的質數中,可以組成一個3階的質數魔方陣。
 請寫程式,找出這個3*3的質數魔方陣。
 輸出參考:
 107,131,29
 11  ,89 ,167
 149,47,71
 (註1:這是另一個小於170質數的質數魔方陣。)

2.觀察下列兩個等式有何規律?
       (1)12 ×42=21 ×24  
       (2)13 ×62=31 ×26
 利用你所發現的規律,請寫程式再找出3個類似的等式(兩數皆為兩位數)!

3.1202年,義大利數學家斐波那契出版了他的「算盤全書」。
他在書中提出了一個關於兔子繁殖的問題:
如果一對兔子每月能生一對小兔(一雄一雌),而每對小兔
在牠出生後的第三個月裡,又能開始生一對小兔,假定在
不發生死亡的情況下,由一對出生的小兔開始,30個月後會有
多少對兔子?


請在時間到時,或是,寫完程式時,將程式及解答一併貼出來。
(只貼一次,若有意外,也請不要刪除前次貼文)

8 則留言:

  1. -1----------------------




    ------------------------
    -2----------------------
    Dim a(90) As Integer
    Private Sub Form_Load()
    Dim t As Boolean
    Me.Hide
    Open App.Path & "\out.txt" For Output As #1
    c = 1
    For j = 1 To 9
    For k = 1 To 9
    For g = 1 To 9
    For h = 1 To 9
    If c <= 3 Then
    If Val(g & h) <> 12 And Val(g & h) <> 42 And Val(g & h) <> 13 And Val(g & h) <> 26 And Val(g & h) <> 63 And Val(g & h) <> 36 And Val(g & h) <> 62 Then
    If Val(g & h) <> Val(j & k) Then
    If Val(g & h) <> Val(k & j) Then
    If g <> h And j <> k Then
    If Val(g & h) * Val(j & k) = Val(h & g) * Val(k & j) Then
    Print #1, (g & h) & "*" & Val(j & k) & "=" & Val(h & g) & "*" & Val(k & j)
    c = c + 1
    End If
    End If
    End If
    End If
    End If
    End If
    Next h
    Next g
    Next k
    Next j
    Close #1
    End
    End Sub

    out:
    84*12=48*21
    93*13=39*31
    82*14=28*41
    ------------------------
    -3----------------------
    Dim a(30) As Long
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #1
    Call born
    Close #1
    End
    End Sub
    Sub born()
    a(1) = 1: a(2) = 1
    For i = 3 To 30
    a(i) = a(i - 1) + a(i - 2)
    Next i
    Print #1, a(30) & "對兔子"
    End Sub

    out:
    832040對兔子
    ------------------------

    回覆刪除
  2. 1.
    Private Sub Form_Load()
    Dim ans(3, 3), x(29)
    Dim a As Boolean
    Me.Hide
    Open App.Path & "\out.txt" For Output As #1
    For i = 3 To 120
    a = True
    For j = 2 To i - 1
    If i Mod j = 0 Then a = False: Exit For
    Next
    If a = True Then n = n + 1: x(n) = i
    Next
    For i = 1 To 29
    For j = i + 1 To 29
    If 82 - (x(i) + x(j)) = 0 Then Print #1, x(i) & " " & x(j)
    Next
    Next
    Close
    End
    End Sub

    2.
    Private Sub Form_Load()
    Dim a As Boolean
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out2.txt" For Output As #2
    n = 0
    For i = 10 To 99
    For j = i To 99
    b = Val(Mid(i, 2, 1) & Mid(i, 1, 1))
    c = (Mid(j, 2, 1) & Mid(j, 1, 1))
    If i * j = b * c Then
    If j / 2 = b Then
    If c / 2 = i And j / 2 <> i Then List1.AddItem i & "*" & j & "=" & b & "*" & c
    End If
    End If
    Next
    Next
    For i = 0 To List1.ListCount - 2
    e = Split(List1.List(i), "=")
    For j = i + 1 To List1.ListCount - 2
    f = Split(List1.List(j), "=")
    If e(0) = f(1) Then List1.RemoveItem j
    Next
    Next
    For i = 0 To 4
    If List1.List(i) <> "" Then n = n + 1: Print #2, "(" & n & ")" & List1.List(i)
    Next
    Close
    Close
    End
    End Sub
    答案:
    (1)12*42=21*24
    (2)13*62=31*26
    (3)14*82=41*28
    (4)23*64=32*46
    (5)24*84=42*48

    3.
    Private Sub Form_Load()
    Dim ans(30) As Double
    Open App.Path & "\out3.txt" For Output As #1
    Me.Hide
    Sum = 2
    x = 2
    ans(1) = 1
    ans(2) = 1
    ans(3) = 2
    For i = 3 To 30
    ans(i) = ans(i - 1) + ans(i - 2)
    Next
    Print #1, ans(30)
    End
    End Sub
    答案: 832040

    回覆刪除
  3. 小冰和哲好,
    兩個都是做了兩題正確的,算是平手。
    但是,小冰的第2題要小心,這樣的答案,有可能會不符合題意,而算錯哦。(因為題目是說「再找3組」,你回答了5組)。
    所以,我想,下個星期再考一次看看吧。
    ***
    另外,第1題的做法,前半,當然是簡單,像小冰的做法就可以。
    然後是要像「老鼠走迷宮」的方式,去試試。
    檢查條件是到了第6步,第7、8 、9步。
    小冰,你前面做質數得到了n, 怎麼不去用n這個變數呢?
    小冰的第3題也是,都寫出了ans(3)了,迴圈怎麼還從3呢?
    ***
    阿哲的第2題,寫條件的順序,可以再想想。
    If Val(g & h) * Val(j & k) = Val(h & g) * Val(k & j) Then
    這一個條件為主吧,那麼其它的就會少很多了吧。
    然後,為了去掉已經出現的那2組,
    是不是放在寫入之前,就行了。
    寫入之前,將式子先組合成字串,再去比那2個字串,就行了吧。
    ***
    再加油。

    回覆刪除
  4. 我第三題是那時候發現是三個月就可以生,而不是每過三個月才可以,所以改程式碼有點急,第一題沒寫完,只停留在找質數,而方法也還在想,所以不完整!
    第二題的規律不只有數字顛倒相乘會等於原數以外!
    應該還有兩個數相差2倍!
    AB*BC=BA*CB AND AB = CB/2: BC = BA/2
    (1)12 ×42=21 ×24 ,24 = 12*2,42=21*2
    (2)13 ×62=31 ×26 ,26 = 13*2,62=31*2
    所以哲第二題應該少了這個規律。

    回覆刪除
  5. 59 53 101
    113 71 29
    41 89 83
    第一題的答案,用暴力下去跑的。
    遞迴還在想方法。
    謝謝老師的鳳梨酥,很好吃。

    回覆刪除
  6. 小冰好,
    雖然你的答案,應該也是正確的。
    (加總是對,也似乎都是質數。)
    但是,我手邊的答案,卻不是這一組。
    每3個數的加總值,你的是213。
    我手邊的是177。
    星期一再看看。
    你也可以將程式也放上來。

    回覆刪除
  7. 發現了一種規律。
    但不確定是不是每個都適用,可能是碰巧的。
    Dim ans(9), x(30), mm As Integer
    Dim ch As Boolean
    Private Sub Form_Load()
    For i = 1 To 9
    ans(i) = 0
    Next
    Me.Hide
    d = 4: c = 1
    Open App.Path & "\out.txt" For Output As #1
    For i = 2 To 120
    b = True
    For j = 2 To i \ 2
    If i Mod j = 0 Then b = False: Exit For
    Next
    If b = True Then n = n + 1: x(n) = i
    Next
    m = (x(30) + x(1)) \ 2
    For i = 1 To 30
    ch = False
    For j = i + 1 To i + 1
    If m >= x(i) And m <= x(j) Then
    a = x(j) - m
    b = m - x(i)
    ch = True
    Exit For
    End If
    Next
    If ch = True Then Exit For
    Next
    If a > b Then
    m = m + b
    Else
    m = m + a
    End If
    For i = 1 To 30
    If m = x(i) Then m = i
    Next
    mm = x(m) * 3
    m1 = (mm / 3) * 2
    For i = 1 To 30
    For j = 1 To 30
    If i <> j And x(i) <> (m1 / 2) And x(j) <> (m1 / 2) Then
    If x(i) + x(j) = m1 Then List1.AddItem x(i)
    End If
    Next
    Next
    ans(5) = (m1 / 2)

    For i = 0 To List1.ListCount - 1
    For i1 = 0 To List1.ListCount - 1
    For i2 = 0 To List1.ListCount - 1
    For i3 = 0 To List1.ListCount - 1
    If i2 <> i3 And i3 <> i1 And i3 <> i Then
    ans(1) = List1.List(i): ans(3) = List1.List(i1)
    ans(7) = List1.List(i2): ans(9) = List1.List(i3)
    If Val(ans(1)) + Val(ans(9)) = Val(ans(3)) + Val(ans(7)) Then
    For k = 1 To 30
    ch = True
    If ch = True Then
    If Val(ans(1)) + Val(ans(3)) + x(k) = mm Then ans(2) = x(k) Else ch = False
    If Val(ans(7)) + Val(ans(9)) + x(k) = mm Then ans(8) = x(k) Else ch = False
    If Val(ans(1)) + Val(ans(7)) + x(k) = mm Then ans(4) = x(k) Else ch = False
    If Val(ans(3)) + Val(ans(9)) + x(k) = mm Then ans(6) = x(k) Else ch = False
    End If
    Next
    Call check
    End If
    End If
    Next
    Next
    Next
    Next
    Close
    End
    End Sub

    Function check()
    ch = True
    If (Val(ans(1)) + Val(ans(2)) + Val(ans(3))) <> mm Then ch = False
    If (Val(ans(4)) + Val(ans(5)) + Val(ans(6))) <> mm Then ch = False
    If (Val(ans(7)) + Val(ans(8)) + Val(ans(9))) <> mm Then ch = False
    If (Val(ans(1)) + Val(ans(4)) + Val(ans(7))) <> mm Then ch = False
    If (Val(ans(2)) + Val(ans(5)) + Val(ans(8))) <> mm Then ch = False
    If (Val(ans(3)) + Val(ans(6)) + Val(ans(9))) <> mm Then ch = False
    If (Val(ans(1)) + Val(ans(5)) + Val(ans(9))) <> mm Then ch = False
    If (Val(ans(3)) + Val(ans(5)) + Val(ans(7))) <> mm Then ch = False

    If ch = True Then
    For i = 1 To 9
    For j = 1 To 9
    If i <> j Then
    If ans(i) = ans(j) Or ans(j) = 0 Or ans(j) > 113 Then ch = False
    End If
    Next
    Next
    End If

    If ch = True Then
    If Str(ans(2)) = Str(ans(1)) Or Str(ans(2)) = Str(ans(3)) Or Str(ans(2)) = Str(ans(7)) Or Str(ans(2)) = Str(ans(9)) Then ch = False
    If Str(ans(4)) = Str(ans(1)) Or Str(ans(4)) = Str(ans(3)) Or Str(ans(4)) = Str(ans(7)) Or Str(ans(4)) = Str(ans(9)) Then ch = False
    If Str(ans(6)) = Str(ans(1)) Or Str(ans(6)) = Str(ans(3)) Or Str(ans(6)) = Str(ans(7)) Or Str(ans(6)) = Str(ans(9)) Then ch = False
    If Str(ans(8)) = Str(ans(1)) Or Str(ans(8)) = Str(ans(3)) Or Str(ans(8)) = Str(ans(7)) Or Str(ans(8)) = Str(ans(9)) Then ch = False
    If ch = True Then
    For i = 1 To 9
    Print #1, ans(i) & " ";
    If i Mod 3 = 0 Then Print #1,
    Next
    Print #1,
    End If
    End If

    End Function

    回覆刪除
  8. 輸出:
    17 113 47
    89 59 29
    71 5 101

    17 89 71
    113 59 5
    47 29 101

    47 113 17
    29 59 89
    101 5 71

    47 29 101
    113 59 5
    17 89 71

    71 89 17
    5 59 113
    101 29 47

    71 5 101
    89 59 29
    17 113 47

    101 29 47
    5 59 113
    71 89 17

    101 5 71
    29 59 89
    47 113 17

    回覆刪除