题目描述
一个数如果恰好等于它的因子之和,这个数就称为"完数"。 例如,6的因子为1、2、3,而6=1+2+3,因此6是"完数"。 编程序找出N之内的所有完数,并按下面格式输出其因子:
输入描述
N
输出描述
? its factors are ? ? ?
样例输入
1000
样例输出
6 its factors are 1 2 3
28 its factors are 1 2 4 7 14
496 its factors are 1 2 4 8 16 31 62 124 248
示例代码
#include<iostream>
using namespace std;
int main()
{
int i, j, sum,N;
cin>>N;
for (i = 2; i <= N; i++)
{
sum = 0;
for (j = 1; j <= i/2 ; j++)//计算某数i的因子之和
if (i%j == 0)
sum += j;
if (sum == i) //判断此数是否为完数
{
cout<<i<<" its factors are ";
for (j = 1; j <=i/2; j++)
if (i%j == 0) cout<<j<<" ";
cout<<endl;
}
}
return 0;
}