2010年6月7日 星期一

vb選手第二次測試

vb選手第二次測試
1.請找出在integer範圍內最大的兩個質數。(10%)
2.請找出在long範圍內最大的兩個質數。(10%)
3.請找出在long範圍內最大的費氏數,並找出它是第幾項。(10%)
 (f1=1,  f2=1,  f3=2,  ......)
4.請試著找出大於long的第一個質數。(10%)
 如果找不出來,也請試著寫寫該如何做,應該可以找出來。
5.數字遊戲,3位數的幾a幾b。(40%)
  例:謎底為175,若猜123,得到1a;若猜456,得到1b;若猜135,得到2a。
  位置相同、數字相同的猜測數字,得到1a;位置不同,但數字相同的猜測數字,得到1b。
  問題5-1,第1個數不可為0的謎底,若有幾組?並將這麼多組的謎底,輸出到out1.txt。
  問題5-2,若猜完123,得到1b。則符合這個猜測的謎底,還有幾組?
           並將這麼多組的謎底,輸出到out2.txt。
6.輸入的in.txt檔,去判斷是否完成"數獨"遊戲。(20%)
  數獨的每個直行、每個橫行都由不重複的1到9的數字組成,
  並且將81格依井字分成9個小區,每個小區也是由不重複的1到9數字組成。
  輸入檔說明:
  第1行的數字,表示有幾組待判斷的數獨解。
  每組數獨解,有9行,每行9個1到9的數字。
  例in.txt:
  2
  123456789
  456789123
  789123456
  231564897
  564897231
  897231564
  312645978
  645978312
  978312645
  123456789
  287654321
  335724689
  412345678
  534567898
  686543292
  754698723
  823445679
  923445678

  輸出說明:
  第1組數獨解:正確
  第2組數獨解:不正確

9 則留言:

  1. 第1題
    32719
    32749
    第2題
    2147483647
    2147483629
    第3題

    第4題
    2147483649
    第5題
    5-1
    648組
    5-2
    228組
    程式碼:
    第1題
    Dim i As Integer
    n = 1
    For i = 32767 To 2 Step -1
    If n > 2 Then Exit For
    check = True
    For j = 2 To i ^ 0.5
    If i Mod j = 0 Then check = False
    Next j
    If check Then
    Print i
    n = n + 1
    End If
    Next i
    第2題
    n = 1
    For i = 2147483647 To 2 Step -1
    If n > 2 Then Exit For
    check = True
    For j = 2 To i ^ 0.5
    If i Mod j = 0 Then check = False
    Next j
    If check Then
    Print i
    n = n + 1
    End If
    Next i
    第3題

    第4題
    X1 = 2147483647
    X2 = 2
    Do
    check = True
    For i = 2 To X2 ^ 0.5
    If X2 Mod i = 0 Then
    check = fasle
    End If
    Next i
    If check Then
    Print X1 + X2
    Exit Do
    End If
    X2 = X2 + 1
    Loop

    回覆刪除
  2. 第5題
    Private Sub Form_Load()
    Open App.Path & "\out1.txt" For Output As #1
    n = 0
    For i = 1 To 9
    For j = 0 To 9
    For k = 0 To 9
    check = True
    If i = j Or i = k Or j = k Then check = False
    If check Then
    n = n + 1
    Print #1, i & j & k
    End If
    Next k
    Next j
    Next i
    Print #1, n
    Close #1
    Open App.Path & "\out2.txt" For Output As #2
    n = 0
    For i = 0 To 9
    For j = 0 To 9
    check1 = True: check2 = True
    If i = j Or i = 1 Or j = 1 Or i = 2 Or j = 2 Or i = 3 Or j = 3 Then
    check1 = False: check2 = False
    End If
    If check1 Then
    Print #2, 2 & i & j
    Print #2, 3 & i & j
    n = n + 2
    End If
    Next j
    Next i
    For i = 1 To 9
    For j = 0 To 9
    check1 = True: check2 = True
    If i = j Or i = 1 Or j = 1 Or i = 2 Or j = 2 Or i = 3 Or j = 3 Then
    check1 = False: check2 = False
    End If
    If check1 Then
    Print #2, i & 1 & j
    Print #2, i & 3 & j
    n = n + 2
    End If
    Next j
    Next i
    For i = 1 To 9
    For j = 0 To 9
    check1 = True: check2 = True
    If i = j Or i = 1 Or j = 1 Or i = 2 Or j = 2 Or i = 3 Or j = 3 Then
    check1 = False: check2 = False
    End If
    If check1 Then
    Print #2, i & j & 1
    Print #2, i & j & 2
    n = n + 2
    End If
    Next j
    Next i
    Print #2, n
    Close #2
    End Sub
    第6題
    Dim S(9) As String
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    For nn = 1 To n
    ReDim m(9, 9) As Integer
    ans = True
    For i = 1 To 9
    Line Input #1, S(i)
    Next i
    For i = 1 To 9
    For j = 1 To 9
    m(i, j) = Mid(S(i), j, 1)
    Next j
    Next i
    For i = 1 To 9
    sum1 = 0: sum2 = 0
    For j = 1 To 9
    cc = 0: bb = 0
    sum1 = sum1 + m(i, j)
    sum2 = sum2 + m(j, i)
    For k = 1 To 9
    If m(i, j) = m(i, k) Then
    cc = cc + 1
    End If
    If m(j, i) = m(k, i) Then
    bb = bb + 1
    End If
    Next k
    If cc = 2 Or bb = 2 Then
    ans = False
    End If
    Next j
    If sum1 <> 45 Or sum2 <> 45 Then
    ans = False
    End If
    Next i
    If ans Then
    Print #2, "第" & nn & "組數獨解:正確"
    Else
    Print #2, "第" & nn & "組數獨解:不正確"
    End If
    Next nn
    Close #1
    Close #2
    End Sub

    回覆刪除
  3. 第一題 32749 ,32719

    Private Sub Form_Load()
    Open App.Path & "\out.txt" For Output As #1
    Max = 32767
    k = 1
    For i = Max To 1 Step -1
    For j = 2 To i ^ 0.5
    If i Mod j = 0 Then
    ans = False
    Exit For
    End If
    ans = True
    Next j
    If ans = True Then
    Print #1, i
    k = k + 1
    End If
    If k = 3 Then Exit Sub
    Next i
    Close #1
    End Sub

    第二題 2147483647 2147483629

    Dim max As Long
    Private Sub Form_Load()
    Open App.Path & "\out.txt" For Output As #1
    max = 2147483647
    k = 1
    For i = max To 1 Step -1
    For j = 2 To i ^ 0.5
    If i Mod j = 0 Then
    ans = False
    Exit For
    End If
    ans = True
    Next j
    If ans = True Then
    Print #1, i
    k = k + 1
    End If
    If k = 3 Then Exit Sub
    Next i
    Close #1
    End Sub

    第三題 46

    Dim x As Long, Face(100) As Long
    Private Sub Form_Load()
    x = 2147483647
    x = x / 1.5
    Face(1) = 1
    Face(2) = 1
    For i = 3 To 100
    Face(i) = Face(i - 1) + Face(i - 2)
    If Face(i) > x Then
    Print Face(i)
    Exit For
    End If
    Next i
    End Sub

    回覆刪除
  4. 第一題:
    32749
    32719
    第二題:
    2147483647
    2147483629
    第三題:
    1836311903
    第四題:

    第五題:
    5-1總共 648 組
    5-2總共 228 組
    第六題:
    正確
    不正確

    回覆刪除
  5. 第一題:
    Private Sub Form_Load()
    Dim ans As Boolean
    num = 0
    For i = 32767 To 2 Step -1
    ans = True
    For j = 2 To i - 1
    If i Mod j = 0 Then ans = False
    Next j
    If ans Then
    num = num + 1
    Print i
    End If
    If num = 2 Then Exit Sub
    Next i
    End Sub
    第二題
    Private Sub Form_Load()
    Dim ans As Boolean
    num = 0
    For i = 2147483647 To 2 Step -1
    ans = True
    For j = i ^ 0.5 To 2 Step -1
    If i Mod j = 0 Then ans = False
    Next j
    If ans Then
    num = num + 1
    Print i
    End If
    If num = 2 Then Exit Sub
    Next i
    End Sub

    第三題
    Private Sub Form_Load()
    Dim qq(100) As Long
    qq(1) = 1
    qq(2) = 1
    For i = 3 To 46
    qq(i) = qq(i - 1) + qq(i - 2)
    k = i
    Next i
    Print qq(k)
    End Sub

    回覆刪除
  6. 第四題



    第五題-1 504
    第五題-2 252

    Private Sub Form_Load()
    Call Q1
    Call Q2
    End Sub

    Public Sub Q1()
    Open App.Path & "\out1.txt" For Output As #1
    For i = 1 To 9
    For k = 1 To 9
    For j = 1 To 9
    If i <> j And k <> j And k <> i Then
    ans = ans + 1
    End If
    Next j
    Next k
    Next i
    Print #1, ans
    Close #1
    End Sub

    Public Sub Q2()
    qqq = 123
    a1 = 1
    a2 = 2
    a3 = 3
    Open App.Path & "\out2.txt" For Output As #1
    For k = 1 To 9
    If k = a1 And k <> a2 And k <> a3 Then
    For j = 0 To 9
    If j <> k And j <> a1 And j <> a2 And j <> a3 Then
    For i = 0 To 9
    If i <> k And i <> j And i <> a1 And i <> a2 And i <> a3 Then
    Print #1, j & i & k
    Print #1, i & k & j
    ans = ans + 2
    End If
    Next i
    End If
    Next j
    End If
    If k = a2 And k <> a1 And k <> a3 Then
    For j = 0 To 9
    If j <> k And j <> a1 And j <> a2 And j <> a3 Then
    For i = 0 To 9
    If i <> k And i <> j And i <> a1 And i <> a2 And i <> a3 Then
    Print #1, i & j & k
    Print #1, k & i & j
    ans = ans + 2
    End If
    Next i
    End If
    Next j
    End If
    If k = a3 And k <> a1 And k <> a2 Then
    For j = 0 To 9
    If j <> k And j <> a1 And j <> a2 And j <> a3 Then
    For i = 0 To 9
    If i <> k And i <> j And i <> a1 And i <> a2 And i <> a3 Then
    Print #1, i & k & j
    Print #1, k & i & j
    ans = ans + 2
    End If
    Next i
    End If
    Next j
    End If
    Next k
    Print ans
    Close #1
    End Sub


    第六題

    Dim B(9, 9) As Integer, a(3, 3) As Integer, s(9) As Integer
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, qqq
    For iii = 1 To qqq
    kkk = 1
    Do
    q = 1
    For iii = 1 To 9
    Line Input #1, strQ
    For j = 1 To 9
    B(j, q) = Val(Mid(strQ, j, 1))
    Next j
    q = q + 1
    Next iii

    For i = 1 To 3
    For j = 1 To 3
    a(2, 2) = B(i * 3 - 1, j * 3 - 1)
    a(1, 1) = B(i * 3 - 2, j * 3 - 2)
    a(1, 2) = B(i * 3 - 2, j * 3 - 1)
    a(1, 3) = B(i * 3 - 2, j * 3)
    a(2, 1) = B(i * 3 - 1, j * 3 - 2)
    a(2, 3) = B(i * 3 - 1, j * 3)
    a(3, 1) = B(i * 3, j * 3 - 2)
    a(3, 2) = B(i * 3, j * 3 - 1)
    a(3, 3) = B(i * 3, j * 3)

    For k = 1 To 3
    For kk = 1 To 3
    o = 1: p = 1
    If a(o, p) = a(k, kk) And o <> k And p <> kk Then
    Print "第" & kkk & "組數獨解:不正確"
    kkk = kkk + 1
    Exit Do
    End If
    o = o + 1
    Next kk
    p = p + 1
    Next k
    Next j
    Next i

    For i = 1 To 9
    For j = 1 To 9
    s(j) = B(i, j)
    Next j
    For j = 1 To 9
    For p = 1 To 9
    If s(j) = s(p) And j <> p Then
    Print "第" & kkk & "組數獨解:不正確"
    kkk = kkk + 1
    Exit Do
    End If
    Next p
    Next j
    Next i

    For i = 1 To 9
    For j = 1 To 9
    s(j) = B(j, i)
    Next j
    For j = 1 To 9
    For p = 1 To 9
    If s(j) = s(p) And j <> p Then
    Print "第" & kkk & "組數獨解:不正確"
    kkk = kkk + 1
    Exit Do
    End If
    Next p
    Next j
    Next i

    Print "第" & kkk & "組數獨解:正確"
    kkk = kkk + 1
    Loop Until kkk = qqq + 1
    Next iii
    Close #2
    Close #1
    End Sub

    回覆刪除
  7. 第四題
    Private Sub Form_Load()
    Dim ans As Boolean
    num = 0
    x = 2147483647
    For i = 2 To x ^ 0.5
    step = x Mod i
    q = i
    q = q + step


    Next i
    End Sub

    第五題:
    5-1
    Private Sub Form_Load()
    Open App.Path & "\out1.txt" For Output As #2

    ans = 0
    For i = 102 To 999
    st = i
    X1 = st Mod 10: st = st \ 10
    X2 = st Mod 10: st = st \ 10
    x3 = st Mod 10

    If (X1 <> X2) And (X1 <> x3) And (X2 <> x3) Then
    ans = ans + 1
    Print #2, i
    End If
    Next i
    Print #2, "總共"; ans; "組"
    Close #2

    End Sub

    5-2

    Private Sub Form_Load()
    Dim num As Integer
    Dim a(3) As Integer
    Dim b(3) As Integer
    x = 123
    num = 0
    Dim ans As Boolean
    a(3) = x Mod 10: x = x \ 10
    a(2) = x Mod 10: x = x \ 10
    a(1) = x Mod 10
    Open App.Path & "\step.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do Until EOF(1)
    Input #1, st
    ste = st
    '位置相同、數字相同的猜測數字,得到1a;位置不同,但數字相同的猜測數字,得到1b
    b(3) = st Mod 10: st = st \ 10
    b(2) = st Mod 10: st = st \ 10
    b(1) = st Mod 10
    k = 0
    For i = 1 To 3
    For j = 1 To 3
    If a(i) = b(j) Then
    k = k + 1
    End If
    Next j
    Next i
    ans = False
    If k = 1 Then
    ans = True
    End If
    For i = 1 To 3
    If a(i) = b(i) Then ans = False: Exit For
    Next i

    If ans Then
    Print #2, ste
    num = num + 1
    End If
    Loop
    Print #2, "總共"; num; "組"
    Close #2
    Close #1
    End Sub
    第六題
    Private Sub Form_Load()
    Dim po As Integer
    Dim ans As Boolean
    Dim num(9, 9) As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, e
    For i = 1 To e
    ans = True
    n = 0
    For j = 0 To 8
    Line Input #1, step
    For k = 0 To 8
    num(n, k) = Val(Mid(step, k + 1, 1))
    Next k
    n = n + 1
    Next j
    For k = 0 To 8
    po = 0
    For j = 0 To 8
    po = po + num(k, j)
    Next j
    If po <> 45 Then ans = False: GoTo here
    Next k
    '----------
    For k = 0 To 8
    po = 0
    For j = 0 To 8
    po = po + num(j, k)
    Next j
    If po <> 45 Then ans = False: GoTo here
    Next k
    Print
    '--------
    t = 0
    o = 0
    Do Until o = 3
    Do Until t = 3
    For k = 0 To 8
    x = k \ 3 + t * 3
    y = k Mod 3 + o * 3
    For j = 0 To 8
    X2 = j \ 3 + t * 3
    Y2 = j Mod 3 + o * 3
    If x = X2 And y = Y2 Then
    Else
    If num(x, y) = num(X2, Y2) Then
    ans = False
    GoTo here
    End If
    End If
    Next j
    Next k
    t = t + 1
    o = o + 1
    Loop
    Loop
    here:
    If ans Then
    Print #2, "正確"
    Else
    Print #2, "不正確"
    End If
    Next i
    Close #1
    Close #2
    End Sub

    回覆刪除
  8. 大質數的部分,用高仔的想法完成如下:
    佈置了一個 listbox,用來放結果。
    第一個按鈕用來先產生幾個接近長整數界限的質數。
    第二個按鈕,以這幾個數為基底,看是不是正確,然後再推斷出超過長整數的質數的正確性。
    而概念中的用一個長整數無法表示的數,於是用兩個長整數來表示,結果再將它用連接符號接起來。
    類似小學時,學直式除法,求商求餘數的方式。
    將這分開的兩個數,先將左數的那個數去取得餘數,
    再將這個餘數接到右邊的數的開頭,於是,
    就完成了大過最大長整數的數的質數問題了。

    Private Sub Command1_Click()
    Dim i As Long, j As Long, x As Long, y As Long
    On Error GoTo Somewrong
    i = 2147483501
    Do
    isp = True
    For j = 3 To i ^ 0.5 Step 2
    If i Mod j = 0 Then isp = False: Exit For
    Next j
    If isp Then List1.AddItem i & " is prime"
    i = i + 2
    Loop



    GoTo Normalend
    Somewrong:
    List1.AddItem i & " is the limit of long"
    Normalend:
    End Sub

    Private Sub Command2_Click()
    Dim i As Long, j As Long, x As Long, y As Long
    Dim z As Long
    On Error GoTo Somewrong2
    'i = 2147483629 is prime
    i = 2147483: x = 501

    Do
    isp = True
    For j = 3 To i - 1 Step 2
    'If i Mod j = 0 Then isp = False: Exit For
    y = i Mod j
    z = y & x
    If z Mod j = 0 Then isp = False: Exit For
    Next j
    If isp Then
    List1.AddItem i & x & " is prime"
    Beep
    End If
    x = x + 2
    Loop Until x > 800



    GoTo Normalend2
    Somewrong2:
    List1.AddItem i & " is the limit of long"
    Normalend2:
    End Sub

    回覆刪除
  9. 2147483543 is prime
    2147483549 is prime
    2147483563 is prime
    2147483579 is prime
    2147483587 is prime
    2147483629 is prime
    2147483647 is prime
    2147483647 is the limit of long
    2147483543 is prime
    2147483549 is prime
    2147483563 is prime
    2147483579 is prime
    2147483587 is prime
    2147483629 is prime
    2147483647 is prime
    2147483659 is prime
    2147483693 is prime
    2147483713 is prime
    2147483743 is prime
    2147483777 is prime
    2147483783 is prime

    回覆刪除