C语言编程,完数问题!

2024-12-04 19:40:24
推荐回答(3个)
回答1:

Private Function isws(m As Integer, splus As String) As Boolean
Dim i%, s%
For i = 1 To m / 2
If m Mod i = 0 Then
s = s + i
End If
Next i
If s = m Then '如果是完数,则重新循环,将各因子找出来
splus = m & "=1" '因为1是所有数的因子,所以先找出来,接下来的循环可以从2开始
For i = 2 To m / 2
If m Mod i = 0 Then
splus = splus & "+" & i
End If
Next i
isws = True
Else
isws = False
End If
End Function

Private Sub Form_Click()
Dim i%, sp$
Print "1000以内的完数为:"
For i = 1 To 1000
If isws(i, sp) Then Print sp 'sp是按地址传递的,所以得到的值是函数isws中最后生成的splus值
Next i
End Sub

绝对正确,给分哦

回答2:

#include
int main()
{int num,k,sum;
for(num=6;num<=1000;num++)
{sum=0;
for(k=1;k if(num%k==0) sum+=k;
if(num==sum)
{ printf("%d its factor are 1",num);

for(k=2;k if(num%k==0) printf(",%d",k);
printf("\n");}
}
return 0;
}

回答3:

#include

int main()
{
int number=1000;
int i=2,k,z,sum=0;

while( i<=number)
{ int j=0,sum=0;

int *p=new int[j];
for(k=i;k>=1;--k)
if(i%k==0)
{ p[j++]=i/k;}

for(z=0;z sum+=p[z];sum-=i;

if(sum==i)
{ cout< for( k=0;k cout< cout< }

delete[] p;
i+=2;
}

return 0;
}