acm--n内のすべての完数と因子を探し出す


/*
*            :
*Copyright(c)2013,           
*All rights reserved.
*    :
*  :   
*    :2013  12  24  
*   :v1.0
*             :
*    :    n
           Sample Input
           1000
*    :               ,      "  "。   ,6    1、2、3, 6=1+2+3,  6 "  "。 
                N       ,           :
*    :? its factors are ? ? ?
            Sample Output
            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;
bool wanshu(int);
int main()
{
	int n,i,j;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		if(wanshu(i))
		{
			cout<<i<<" its factors are ";
			for(j=1;j<=i/2;j++)
			{
				if(i%j==0)
				{
					cout<<j<<" ";
				}
			}
			cout<<endl;
		}
	}
	return 0;
}
bool wanshu(int n)
{
	bool flag=false;
	int i,sum=0;
	for(i=1;i<=n/2;i++)
	{
		if(n%i==0)
		{
			sum+=i;
		}
	}
	if(sum==n)
	{
		flag=true;
	}
	return flag;
}

実行結果:
心得: