完数

题目描述

一个数如果恰好等于它的因子之和,这个数就称为"完数"。 例如,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<stdio.h>
#include<math.h>
void main()
{
    int i,j,k,s1=0,s2=0,l=0,n,m=-1,q,a[100],b[100];
    scanf("%d",&q);
 
    for(n=5; n<=q; n++)
    {
        a[0]=1;
        s1=0;
        s2=0;
        l=0;
        m=-1;
        for(i=2; i<=sqrt(n); i++)
        {
            if(n%i==0)
            {
                j=n/i;
                a[++l]=i;
                b[++m]=j;
                s2+=b[m];
                s1+=a[l];
                //  printf("%d\n",i);
            }
        }
        if(s1+s2+a[0]==n)
        {
            if(n==6)
            {
                printf("%d its factors are ",n);
                for(i=0; i<=l; i++)
                    printf("%d ",a[i]);
                for(i=m; i>=0; i--)
                    printf("%d ",b[i]);
 
 
            }
            if(n>6)
            {printf("\n");
                  printf("%d its factors are ",n);
                for(i=0; i<=l; i++)
                    printf("%d ",a[i]);
                for(i=m; i>=0; i--)
                    printf("%d ",b[i]);
 
            }
 
        }
    }
 
}
posted @ 2015-01-08 16:15  zach96  阅读(292)  评论(0编辑  收藏  举报