完数,等于其因子和,如6=1+2+3,求出1000内所有完数,且输出为如6,its all factors are 1,2,3.
#include "stdafx.h"
#include
using namespace std;
int main()
{
int arr[1000] = { 0 };
int x, i, sum;
x = 1;
while (x <= 1000) //x要从1到1000循环。
{
sum = 0; //每算一个新的x时,sum要清0
for (i = 1; i < x; i++)
{
if (x%i == 0)
{
arr[i - 1] = i;
sum += i; //如果i是x的因子,sum加上i
}
}
i = 0;
if (x == sum) //如果因子和等于本身
{
cout << x << "\t,its all factors are ";
while (i < 1000 && arr[i] != 0)
{
cout << arr[i++] << " ";
}
cout << endl;
memset(arr, 0, 1000);
}
x++;
}
getchar();
return 0;
}
#include<iostream.h>
void main()
{
int m;
for(m=1;m<=1000;m++)
{
int k=0;
for(int i=1;i<m;i++)//这里修改了
{
if((m%i)==0)
k=k+i;
}
if(k==m)
cout<<" "<<m<<endl;
}
}