求 1000以内的完全数 的VB代码!!
答案:3 悬赏:50 手机版
解决时间 2021-02-23 19:58
- 提问者网友:人生佛魔见
- 2021-02-23 06:10
求 1000以内的完全数 的VB代码!!
最佳答案
- 五星知识达人网友:你可爱的野爹
- 2021-02-23 07:41
Option Explicit
Option Base 1
Private Sub command1_click()
Dim I As Integer, A() As Integer
Dim K As Integer, sum1 As Integer, sum2 As Integer
For I = 2 To 1000
sum1 = 0: sum2 = 0
Call fctor(I, sum1)
Call fctor(sum1, sum2)
If sum2 = 2 * I Then
K = K + 1
ReDim Preserve A(K)
A(K) = I
End If
Next I
For I = 1 To K
Text1.Text = Text1.Text & Str(A(I)) & " "
Next I
End Sub
Private Sub fctor(N As Integer, S As Integer)
Dim I As Integer, J As Integer
Do While I <= N
I = I + 1
If N Mod I = 0 Then
S = S + I
End If
Loop
End Sub
Option Base 1
Private Sub command1_click()
Dim I As Integer, A() As Integer
Dim K As Integer, sum1 As Integer, sum2 As Integer
For I = 2 To 1000
sum1 = 0: sum2 = 0
Call fctor(I, sum1)
Call fctor(sum1, sum2)
If sum2 = 2 * I Then
K = K + 1
ReDim Preserve A(K)
A(K) = I
End If
Next I
For I = 1 To K
Text1.Text = Text1.Text & Str(A(I)) & " "
Next I
End Sub
Private Sub fctor(N As Integer, S As Integer)
Dim I As Integer, J As Integer
Do While I <= N
I = I + 1
If N Mod I = 0 Then
S = S + I
End If
Loop
End Sub
全部回答
- 1楼网友:duile
- 2021-02-23 09:14
dim i,j,a as integer
for i=2 to 1000
a=0
for j=1 to i-1
if i mod j=0 then a=a+j'判断j是否能整除i,如果能,则a增加j
next j
if a=i then print i'判断i是否是完全数,如果是,则输出i
next i
运行输出:
6
28
496
for i=2 to 1000
a=0
for j=1 to i-1
if i mod j=0 then a=a+j'判断j是否能整除i,如果能,则a增加j
next j
if a=i then print i'判断i是否是完全数,如果是,则输出i
next i
运行输出:
6
28
496
- 2楼网友:动情书生
- 2021-02-23 09:03
哈哈
GamesBug.NO2身手好快!我刚刚复制好代码,你已经提交了……
GamesBug.NO2身手好快!我刚刚复制好代码,你已经提交了……
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯