2012年7月28日 星期六

99模擬 Problem 5-2


子題2(8%):如果有一個正整數n,其值等於所有n 的因數(除了n 以外)之總合,則n 稱為「完美數」(Perfect number)。在此計算中,其「因數」不限制為「質因數」。請輸出2 到數字k 之間的完美數。
輸入說明:
第1 行有1 個數字,代表k 的值。而k 的值不超過50000。
輸出說明:
每行輸出1 個範圍內的完美數,依其值由小到大輸出。
輸入範例:【檔名:in-5-2.txt】
10000
輸出範例:【檔名:out-5-2.txt】
6
28
496
8128

3 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Dim a As Long
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x

    For i = 2 To x
    a = 0
    For j = 1 To (i \ 2)
    If i Mod j = 0 Then a = a + j
    Next
    If a = i Then Print #2, i
    Next

    Close
    Close
    End
    End Sub

    回覆刪除
  2. 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 As Long
    Input #1, n
    For i = 2 To n
    x = 0
    For j = 1 To (i \ 2)
    If i Mod j = 0 Then x = x + j
    Next j
    If x = i Then Print #2, i
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. 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, x
    For i = 2 To Val(x)
    Call a(Val(i))
    Next i
    Close #2
    Close #1
    End
    End Sub

    Sub a(b)
    ans = 1
    For i = 2 To b / 2 - 1
    If d = i Then Exit For
    If b Mod i = 0 Then
    c = i: d = b / i
    ans = ans + c + d
    End If
    Next i
    If ans = b Then Print #2, b
    End Sub

    回覆刪除