1017: C语言程序设计教程(第三版)课后习题6.7

题目描述

一个数如果恰好等于它的因子之和,这个数就称为"完数"。 例如,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 
 3 int is_wan(int n)
 4 {
 5     int i, s = 0;
 6     
 7     for(i = 1; i < n; i++)
 8     {
 9         if(n % i == 0)
10             s += i;
11     }
12 
13     if(s == n)
14         return 1;
15     else
16         return 0;
17 }
18 
19 int main(int argc, char const *argv[])
20 {
21     int i, n, j;
22     scanf("%d", &n);
23     for(i = 2; i < n; i++)
24     {
25         if(is_wan(i) == 1)
26         {
27             printf("%d its factors are ", i);
28             for(j = 1; j < i; j ++)
29             {
30                 if (i % j == 0)
31                 {
32                     if(j == 1)
33                         printf("%d", j);
34                     else
35                         printf(" %d", j);
36                 }
37             }
38             printf("
");
39         }
40     }
41     return 0;
42 }
原文地址:https://www.cnblogs.com/hello-lijj/p/7828175.html