早教吧 育儿知识 作业答案 考试题库 百科 知识分享

找出2~10000之内的所有完全数.所谓完全数,就是这个数的各因子之和恰好等于这个数本身,如6=1+2+3,28=1+Dima,b,cAsIntegerFora=2To10000c=0Forb=1Toa-1IfaModb=0Thenc=c+bEndIfNextIfa=cThenMsgBo

题目详情
找出2~10000之内的所有完全数.所谓完全数,就是这个数的各因子之和恰好等于这个数本身,如6=1+2+3,28=1+
Dim a,b,c As Integer
For a = 2 To 10000
c = 0
For b = 1 To a - 1
If a Mod b = 0 Then
c = c + b
End If
Next
If a = c Then
MsgBox(a)
End If
Next
End Sub 这样可以求出,但不能全部输出,点一次确定才输出一个,怎么改才能全部输出完全数
▼优质解答
答案和解析
Sub calc_n()
Dim d() As Integer
Dim a, b, c As Integer
Dim str_msg As String
n = 0
str_msg = ""
For a = 2 To 10000
c = 0
For b = 1 To a - 1
If a Mod b = 0 Then
c = c + b
End If
Next
If a = c Then
ReDim Preserve d(n) As Integer
d(n) = c
str_msg = str_msg & CStr(d(n)) & Chr(13)
n = n + 1
End If
Next
MsgBox str_msg
End Sub