一个数如果恰好等于它的因子之和,这个数就称为“完数”,例如,6的因子为1,2,3,而6=1+2+3,因此6是“完数”。编程序找出1000之内的所有完数,并按下面格式输出其因子:
6,its factors are 1,2,3
#include
#include<math.h>
using namespace std;
int main()
{
int x, s;
for(x=2;x<=1000;x++){
int a[x]={0};
int j=0,i;
s=0;
for(i=1;i<x;i++){
if(x%i0){
s=s+i;
a[j]=i;
j++;
}
}
if(sx){
cout<<x<<", its factors are "<<a[0];
for(int k=1;a[k]!=0;k++){
cout<<’,’<<a[k];
}
cout<<’\n’;
}
}
return 0;
}