[题目]设n是一个正整数,d是它的因子,若d和n/d互质(即d和n/d的最大公约数是1),则称d为n的酉因子。如果n等于它的酉因子和(不包括n自身),则称n为酉完全数。编程找出200以内的酉完全数。
[编程要求]
1. 程序参考界面如图所示,编程时不得增加或减少界面对象或改变对象的种类,窗体及界面元素大小适中,且均可见;
2. 单击“生成酉完全数”按钮,则找出酉完全数,并将找到的酉完全数及其酉因子按图示的格式显示在图片框中;
3. 按“清除”按钮,则将图片框清空;按“退出”按钮,则退出程序执行;
4. 程序中至少要有一个通用过程。Private Sub Command1_Click()'生成酉完全数
Dim n As Integer, d As Integer, x As Integer, y As Integer, sum As Integer, r As Integer
For n = 6 To 200
sum = 0
For d = 1 To (n - 1)
If (n Mod d) = 0 Then
x = n / d
If hcf(d,x) = 1 Then
   sum = sum + d
End If
End If
Next d
If sum = n Then
   Picture1.Print n
End If
Next n
End SubPrivate Sub Command2_Click()'清除
Picture1.Cls
End SubPrivate Sub Command3_Click()'退出
End
End SubFunction hcf(m As Integer, n As Integer) As Integer '求出两个数的最大公约数
Dim r As Integer
r = m Mod n
Do While r <> 0
m = n
n = r
r = m Mod n
Loop
hcf = n
End Function

解决方案 »

  1.   

    Private Sub Command1_Click() '生成酉完全数
    Picture1.AutoRedraw = True
    Function hcf(byal m As Integer, byval n As Integer) As Integer '求出两个数的最大公约数
      

  2.   

    只要便利一半数就可以
    command1:Dim n As Integer, d As Integer, x As Integer, y As Integer, sum As Integer, r As Integer
    For n = 6 To 200
    sum = 1
    For d = 2 To Int(Sqr(n))
    If (n Mod d) = 0 Then
    x = n / d
    If hcf(d, x) = 1 Then
       sum = sum + d + x
    End If
    End If
    Next d
    If sum = n Then
       Picture1.Print n
    End If
    Next n
      

  3.   

    hcf重新声明一下,要用传值方式,否则会影响入参Function hcf(ByVal m As Integer, ByVal n As Integer) As Integer