2012年11月20日 星期二

5/21程式小考_3rd


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

3 則留言:

  1. Dim num, ans As Integer
    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, num
    For i = 2 To num
    For ii = 1 To i - 1
    If i Mod ii = 0 Then ans = ans + ii
    Next
    If ans = i Then Print #2, i
    ans = 0
    Next
    Close #2
    Close #1
    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
    Input #1, x
    For i = 1 To x
    s = 0
    For j = 1 To i - 1
    If i Mod j = 0 Then s = s + j
    Next
    If s = i Then Print #2, i
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  3. Dim num As Integer
    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 = 4 To n
    For j = 2 To i - 1
    If i Mod j = 0 Then num = num + j
    Next
    If num + 1 = i Then Print #2, i
    num = 0
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除