2012年5月21日 星期一

5/21程式小考


※請使用下載文件內之in.txt作答,否則不予給分。
http://192.168.1.130/

1.        輸入一數n,請輸出n數之後的下一個質數。
輸入範例:6 
               45
輸出範例:7
        47

2.        讀入兩數,並將兩數字間所有的數字各自拆開,並輸出最多者與其各數。
    (若個數相等,則輸出數字最大者)
輸入範例:10 12
輸出範例:1 4

3.        請找出n以下的完全數並輸出。
    完全數定義: 一個數的真因數和,除了本身以外的正因數和,剛好等於它本身, 則稱此數為完全數。
輸入範例:100
輸出範例: 6
               28
         

4.        輸入MN兩陣列,其中M為姓名N為成績,請按照成績高低排序並輸出。
(若發現成績相同,則照字母順序排列)
輸入一: Ezreal
             Ashe
             Leona
             Lux
輸入二: 80
                     20
                     75
                     89
  輸出範例:  
Lux 89
Ezreal 80
Leona 75
Ashe 20

5.        三顆公正的骰子,請輸出總和為n的所有組合。
輸入範例: 3
輸出範例: 1 1 1





18 則留言:

  1. 第一題
    Private Sub Form_Load()
    Me.Hide
    Dim x, y As Long
    Dim a As Boolean
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out1.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, x
    Do
    a = True
    For i = x + 1 To x + 10000
    For j = 2 To x - 1
    If i Mod j = 0 Then
    a = False
    Exit For
    End If
    Next
    If a = True Then
    Print #2, i
    Exit For
    End If
    Next
    If a = False Then x = x + 1
    Loop Until a = True
    Loop
    Close
    Close
    End
    End Sub
    第二題
    Private Sub Form_Load()
    Me.Hide
    Dim a(9)
    Open App.Path & "\in2.txt" For Input As #1
    Open App.Path & "\out2.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, x, y
    For i = 0 To 9
    a(i) = 0
    Next
    For i = x To y
    For j = 1 To Len(i)
    Select Case Mid(i, j, 1)
    Case "0"
    a(0) = a(0) + 1
    Case "1"
    a(1) = a(1) + 1
    Case "2"
    a(2) = a(2) + 1
    Case "3"
    a(3) = a(3) + 1
    Case "4"
    a(4) = a(4) + 1
    Case "5"
    a(5) = a(5) + 1
    Case "6"
    a(6) = a(6) + 1
    Case "7"
    a(7) = a(7) + 1
    Case "8"
    a(8) = a(8) + 1
    Case "9"
    a(9) = a(9) + 1
    End Select
    Next
    Next
    For i = 0 To 9
    If a(i) >= Max Then
    Max = a(i)
    ans = i
    End If
    Next
    Print #2, ans & " " & Max
    Loop
    Close
    Close
    End
    End Sub
    第三題
    Private Sub Form_Load()
    Me.Hide
    Dim ans, n As Long
    Open App.Path & "\in3.txt" For Input As #1
    Open App.Path & "\out3.txt" For Output As #2
    Input #1, n
    For i = 1 To n
    ans = 0
    For j = 1 To i - 1
    If i Mod j = 0 Then ans = ans + j
    Next
    If ans = i Then Print #2, ans
    Next
    Close
    Close
    End
    End Sub
    第四題
    Private Sub Form_Load()
    Dim n(1000), n1(1000)
    Me.Hide
    Open App.Path & "\in4-1.txt" For Input As #1
    Open App.Path & "\out4.txt" For Output As #2
    Open App.Path & "\in4-2.txt" For Input As #3
    a = 0
    Do While Not EOF(1)
    a = a + 1
    Input #1, na
    n(a) = na
    Loop
    a = 0
    Do While Not EOF(3)
    Input #3, gr
    a = a + 1
    n1(a) = Val(gr)
    Loop
    For i = 1 To UBound(n)
    For j = 1 To UBound(n) - 1
    If n1(j) < n1(j + 1) Then
    b = n1(j)
    n1(j) = n1(j + 1)
    n1(j + 1) = b
    b = n(j)
    n(j) = n(j + 1)
    n(j + 1) = b
    End If
    Next
    Next
    For i = 1 To UBound(n)
    Print #2, n(i) & " " & n1(i)
    Next
    Close
    Close
    Close
    End
    End Sub

    第五題

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in5.txt" For Input As #1
    Open App.Path & "\out5.txt" For Output As #2
    Input #1, x
    For i = 1 To 6
    For j = 1 To 6
    For k = 1 To 6
    If i + j + k = x Then Print #2, i; j; k
    Next
    Next
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  2. 第一題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim a As Long
    Do While Not EOF(1)
    Input #1, n
    For i = n + 1 To 2 * n
    a = 0
    For j = 2 To i - 1
    If i Mod j <> 0 Then a = a + 1
    Next j
    If a = i - 2 Then
    Print #2, i
    Exit For
    End If
    Next i
    Loop
    Close
    Close
    End
    End Sub
    ----------------------------------------------------------
    第三題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in3.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim a As Integer
    Do While Not EOF(1)
    a = 0
    Input #1, n
    For i = 1 To n
    For j = 1 To n / 2
    'For k = 1 To (n / 2 - 1)
    If i Mod j = 0 Then a = a + j
    If i = a Then Print #2, a
    'Next k
    Next j
    Next i
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除
  3. 第一題
    Private Sub Form_Load()
    Me.Hide
    Dim t As Boolean
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out1.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, n
    Do
    t = True
    n = n + 1
    For i = 2 To n - 1
    If n Mod i = 0 Then t = False
    Next i
    Loop Until t = True
    If t = True Then Print #2, n
    Loop
    Close #2
    Close #1
    End
    End Sub
    第二題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in2.txt" For Input As #1
    Open App.Path & "\out2.txt" For Output As #2
    Dim c1(100), c2(100) As Integer
    Do While Not EOF(1)
    Input #1, x, y
    a = Len(x)
    b = Len(y)
    k = a + b
    t = 1
    Max = 0
    For i = 1 To a
    c1(i) = Val(Mid(x, i, 1))
    For j = 1 To b
    c2(j) = Val(Mid(y, j, 1))
    If c1(i) > Max Then Max = c1(i)
    If c2(j) > Max Then Max = c2(j)
    If c1(i) = c2(j) Then t = t + 1: ans = c1(i)
    Next j
    Next i
    If t = 1 Then
    Print #2, Max, k
    Else
    Print #2, ans, k
    End If
    Loop
    Close #2
    Close #1
    End
    End Sub
    第三題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in3.txt" For Input As #1
    Open App.Path & "\out3.txt" For Output As #2
    Input #1, n
    For i = 2 To n
    ans = 0
    For j = 1 To i - 1
    If i Mod j = 0 Then ans = ans + j
    Next j
    If ans = i Then Print #2, ans
    Next i
    Close #2
    Close #1
    End
    End Sub
    第五題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in5.txt" For Input As #1
    Open App.Path & "\out5.txt " For Output As #2
    Dim a(216) As String
    Dim b(216) As Integer
    Input #1, n
    c = 1
    For i = 1 To 6
    For j = 1 To 6
    For k = 1 To 6
    a(c) = i & " " & j & " " & k
    b(c) = i + j + k
    c = c + 1
    Next k
    Next j
    Next i
    For i = 1 To 216
    If n = b(i) Then Print #2, a(i)
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. 第一題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim a As Long
    Do While Not EOF(1)
    Input #1, n
    For i = n + 1 To 2 * n
    a = 0
    For j = 2 To i - 1
    If i Mod j <> 0 Then a = a + 1
    Next j
    If a = i - 2 Then
    Print #2, i
    Exit For
    End If
    Next i
    Loop
    Close
    Close
    End
    End Sub
    ------------------------------------
    第二題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in2.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Dim a(9) As Integer
    Input #1, x, y
    For i = x To y
    z = Len(i)
    For j = 1 To z
    Select Case Mid(i, j)
    Case 0
    a(0) = a(0) + 1
    Case 1
    a(1) = a(1) + 1
    Case 2
    a(2) = a(2) + 1
    Case 3
    a(3) = a(3) + 1
    Case 4
    a(4) = a(4) + 1
    Case 5
    a(5) = a(5) + 1
    Case 6
    a(6) = a(6) + 1
    Case 7
    a(7) = a(7) + 1
    Case 8
    a(8) = a(3) + 1
    Case 9
    a(9) = a(9) + 1
    End Select
    Next j
    Next i
    For b = 0 To 9
    For c = 0 To 8
    If a(c) > a(c + 1) Then
    g = a(c + 1)
    a(c + 1) = a(c)
    a(c) = g
    Next c
    Next b
    Print #2, a(0)
    Loop
    Close
    Close
    End
    End Sub
    -----------------------------------------
    第三題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in3.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim a As Integer
    Do While Not EOF(1)
    Input #1, n
    For i = 1 To n
    For j = 1 To i / 2
    If i Mod j = 0 Then a = a + j
    Next j
    If i = a Then Print #2, i
    Next i
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除
  5. 小冰、晟晟、哲好,
    題目 12345
    小冰 O O O O O
    晟晟 O X X X X
    哲  O X O X O
    所以,小冰100分,晟晟20分,哲60分。
    小冰的第1題,是有些小問題的,但是,輸出的結果,卻又是對的。
    5/28我去開程式設計的規則會議,再下個星期一再來討論。
    哲、晟晟,要將錯的題目重做後,也再貼在這兒。
    我會努力一些的,但是,你們更要在一起加油。
    熊掌

    回覆刪除
  6. 6/4那天是校內程式設計比賽,所以那天應該無法討論。
    第一題,因為在寫的時候有點卡住,一直寫錯,所以最後直接想,N之後的一萬個數字裡面不可能會沒有質數,而題目又是要求N後的第一個質數,算是有點賭運氣。

    回覆刪除
  7. 小冰好,
    不只是那個10000的問題,你那個 x=x+1 再回頭的那地方,也怪怪的。
    另外,題目是請arro學長打的,第1題本來是還要先看本身是不是質數的。
    還有,最後一題,本來也是該將順序不同的相同組合給去掉的。
    這是組合,不是排列。
    大家一起加油哦。

    回覆刪除
  8. 那我再改看看,把第一題跟第五題重新做一次。

    回覆刪除
  9. 第一題:(有判斷自己是不是質數,是的話輸出自己)
    Private Sub Form_Load()
    Me.Hide
    Dim x, y As Long
    Dim a As Boolean
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out1.txt" For Output As #2
    Do While Not EOF(1)
    a = True
    Input #1, n
    Do
    For i = 2 To n - 1
    If n Mod i <> 0 Then
    a = True
    Else
    a = False
    Exit For
    End If
    Next
    If a = True Then
    Print #2, n
    Else
    n = n + 1
    End If
    Loop Until a = True
    Loop
    Close
    Close
    End
    End Sub
    第一題:(沒判斷自己是不是質數,直接從n+1下去找)
    Private Sub Form_Load()
    Me.Hide
    Dim x, y As Long
    Dim a As Boolean
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out1.txt" For Output As #2
    Do While Not EOF(1)
    a = True
    Input #1, n
    n = n + 1
    Do
    For i = 2 To n - 1
    If n Mod i <> 0 Then
    a = True
    Else
    a = False
    Exit For
    End If
    Next
    If a = True Then
    Print #2, n
    Else
    n = n + 1
    End If
    Loop Until a = True
    Loop
    Close
    Close
    End
    End Sub

    第五題:
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in5.txt" For Input As #1
    Open App.Path & "\out5.txt" For Output As #2
    Input #1, x
    For i = 1 To 6
    For j = i To 6
    For k = j To 6
    If i + k + j = x Then Print #2, i & j & k
    Next
    Next
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  10. 再回頭寫一次,感覺比較簡單了。

    回覆刪除
  11. 2.
    Private Sub Form_Load()
    Me.Hide
    Dim a(9) As Integer
    Open App.Path & "\in2.txt" For Input As #1
    Open App.Path & "\out2.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, x, y
    Max = 0
    ans = 0
    For i = 0 To 9
    a(i) = 0
    Next i
    For i = x To y
    For j = 1 To Len(i)
    c = Val(Mid(i, j, 1))
    For k = 0 To 9
    If c = k Then a(k) = a(k) + 1
    If a(k) > Max Then Max = a(k): ans = k
    Next k
    Next j
    Next i
    Print #2, ans, Max
    Loop
    Close #2
    Close #1
    End
    End Sub
    5.
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in5.txt" For Input As #1
    Open App.Path & "\out5.txt " For Output As #2
    Dim a(216) As String
    Dim b(216) As Integer
    Input #1, n
    c = 1
    For i = 1 To 6
    For j = i To 6
    For k = j To 6
    a(c) = i & " " & j & " " & k
    b(c) = i + j + k
    c = c + 1
    Next k
    Next j
    Next i
    For i = 1 To 216
    If n = b(i) Then Print #2, a(i)
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  12. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in4-1.txt" For Input As #1
    Open App.Path & "\in4-2.txt " For Input As #2
    Open App.Path & "\out4.txt" For Output As #3
    Dim n(100), s(100) As Integer
    i = 1
    Do Until EOF(1)
    Input #1, n(i)
    i = i + 1
    Loop
    ii = 1
    Do Until EOF(2)
    Input #2, s(ii)
    ii = ii + 1
    Loop
    ' i = ii , i = 25
    For i = 1 To UBound(n)
    For j = 1 To UBound(n) - 1
    If s(j) < s(j + 1) Then
    b = s(j)
    s(j) = s(j + 1)
    s(j + 1) = b
    b = n(j)
    n(j) = n(j + 1)
    n(j + 1) = b
    End If
    Next j
    Next i
    For i = 1 To UBound(n)
    Print #3, n(i) & " " & s(i)
    Next i
    Close #3
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  13. 第二題
    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 a(9) As Integer
    Do While Not EOF(1)
    For C = 0 To 9
    a(C) = 0
    Next C
    Input #1, x, y
    For i = x To y
    For j = 1 To Len(i)
    Select Case Mid(i, j, 1)
    Case "0"
    a(0) = a(0) + 1
    Case "1"
    a(1) = a(1) + 1
    Case "2"
    a(2) = a(2) + 1
    Case "3"
    a(3) = a(3) + 1
    Case "4"
    a(4) = a(4) + 1
    Case "5"
    a(5) = a(5) + 1
    Case "6"
    a(6) = a(6) + 1
    Case "7"
    a(7) = a(7) + 1
    Case "8"
    a(8) = a(8) + 1
    Case "9"
    a(9) = a(9) + 1
    End Select
    Next j
    Next i
    g = 0
    For k = 0 To 9
    If a(k) >= g Then
    g = a(k)
    N = k
    End If
    Next k
    Print #2, N, g
    Loop
    Close
    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 x
    Do While Not EOF(1)
    Input #1, n
    For i = 1 To n
    x = 0
    For j = 1 To i - 1
    If i Mod j = 0 Then
    x = x + j
    End If
    Next j
    If x = i Then Print #2, x
    Next i
    Loop
    Close
    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
    Input #1, n
    For i = 1 To 6
    For j = 1 To 6
    For k = 1 To 6
    If i + j + k = n Then
    Print #2, i; j; k
    End If
    Next k
    Next j
    Next i
    Close
    Close
    End
    End Sub

    第四題還在補

    回覆刪除
  14. 第四題
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in4-1.txt" For Input As #1
    Open App.Path & "\in4-2.txt" For Input As #2
    Open App.Path & "\out.txt" For Output As #3
    Dim a, s(100) As Integer
    Dim n(100), c(100) As String
    a = 0
    Do While Not EOF(1)
    a = a + 1
    Input #1, x
    n(a) = x
    Input #2, y
    s(a) = y
    Loop

    For i = 1 To a
    For j = 1 To a - 1
    If Val(s(j)) < Val(s(j + 1)) Then
    h = s(j)
    s(j) = s(j + 1)
    s(j + 1) = h
    g = n(j)
    n(j) = n(j + 1)
    n(j + 1) = g
    End If
    Next j
    Next i
    For i = 1 To a
    c(i) = n(i) & s(i)
    Print #3, c(i)
    Next i
    Close
    Close
    Close
    End
    End Sub

    回覆刪除
  15. 小冰好,
    第一題正確,改進點:
    If n Mod i <> 0 Then
    a = True
    Else
    a = False
    Exit For
    End If
    這兒先在迴圈外,設定a = true
    然後這兒就只剩下後半
    if n mod i = 0 then
    a = false
    exit for
    end if
    ***
    第五題正確。
    利用3層迴圈,每一層比上一層大於等於,免除了重複的情況,很好。
    (雖然覺得有些危險的感覺)

    回覆刪除
  16. 哲好,
    第2題,第4題,第5題,答案都正確。
    但是,

    改進1:你(們都)是用len(i)來決定數字長度,這不好,如果i先定義了integer 那麼,長度只能是2。
    要拆每個數字,請用mod 10, 再將原數 \ 10就可以拆出每個數字。

    改進2:計次的部分,直接
    a(c)=a(c)+1
    反正那個c,就是陣列中第c個,不用判斷,不用迴圈,直接就是一行就解決了。

    改進3:你的第5題,是有將重複的給除去了,那麼,總共有216種可能嗎?不是吧。

    改進4:你在第4題中,用了i和ii來計次,那麼後頭排序時,就不需要用ubound(n)這個函數來解決已經知道的數字吧。那不就是ii嗎?(而且,你的i用了兩次,在不同的做用裡,很危險)

    回覆刪除
  17. 晟晟好,
    第二題,正確。但是,太辛苦了,參考前面我說給哲的改進2。
    第三題,正確。
    第五題,也算正確啦,如果題目是允許重複的情形的話。但是也要去試試如何將重複的組合給剔除哦。

    回覆刪除
  18. 改進1、改進2、改進4原理知道了。
    那改進3我應該在判斷n的組合要改成這樣吧?
    (迴圈1~所有組合個數)
    For g = 1 To (i - 1) * (j - 1) * (k - 1)
    If n = b(g) Then Print #2, a(g)
    Next g

    回覆刪除