C刷题记录-1017

题目描述

一个数如果恰好等于不包含它本身所有因子之和,这个数就称为"完数"。 例如,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 

 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 int main(){
 5   int i,n,j,k,count,factor_sum=0;
 6   scanf("%d",&n);
 7   for(i = 2; i < n; i++){
 8      int arr[100]={0};
 9      count = 0;factor_sum = 0;
10      for(j = 1;j < i; j++){
11         if (i % j == 0)
12         {
13            arr[count] = j;
14            count++;
15            factor_sum += j;
16         }
17      }
18      if (factor_sum == i)
19      {
20         printf("%d its factors are ",i);
21         for(k = 0; k < count; k++)
22         {
23           printf("%d ",arr[k]);
24         }
25         printf("
");
26      }
27   }
28   return 0;
29 }
原文地址:https://www.cnblogs.com/xiangxyq/p/7808663.html