Fork me on GitHub

完数

一个数如果恰好等于它的因子之和,这个数就称为"完数"。 例如,6的因子为1、2、3,而6=1+2+3,因此6是"完数"。 编程序找出N之内的所有完数,并按下面格式输出其因子:

输入

N

输出

? its fastors are ? ? ?

样例输入

1000

样例输出

6 its fastors are 1 2 3
28 its fastors are 1 2 4 7 14
496 its fastors are 1 2 4 8 16 31 62 124 248
#include <stdio.h>
int main(){
int n, i, i2, j;
int a[] = {6, 28, 496, 8128, 33550336};
while (scanf("%d", &n) == 1){
   for (i=0; i<5; i++){
    if (a[i] <= n){
     printf("%d ", a[i]);
     printf("its fastors are 1 ");
     i2 = a[i]/2;
     for (j=2; j<=i2; j++){
      if (a[i]%j == 0){
          if(j!=i2){
       printf("%d ", j);
             }
          else
          {
         printf("%d", j);
          }
      }
     }
     printf("\n");
    }
   }
}
return 0;
}

 

 
 
posted @ 2018-10-23 18:09  Lazy.Cat  阅读(837)  评论(0编辑  收藏  举报