/*Description
一个数如果恰好等于它的因子之和,这个数就称为"完数"。 例如,6的因子为1、2、3,而6=1+2+3,因此6是"完数"。 编程序找出N之内的所有完数,并按下面格式输出其因子:
Input
N
Output
? its factors are ? ? ?
Sample Input
1000
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
HINT*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int i,j,s=0;
scanf("%d",&n);
for(i=6;i<=n;i++)
{
s=0; //每次循环结束s都要重新归零
for(j=1;j<i;j++)
{
if(i%j==0)
s=s+j;
}
if(s==i)
{
printf("%d its factors are ",s);
for(j=1;j<i;j++)
{
if(i%j==0)
printf("%d ",j);
}
printf("\n");
}
}
return 0;
}
运行结果:计161_Problem : 找出N之内的所有完数
最新推荐文章于 2022-12-05 17:16:46 发布
本文介绍了一个用于寻找指定范围内所有完数的C语言程序。完数是指一个正整数恰好等于其所有真因子(不包括自身)之和。程序通过两层循环结构实现,外层循环遍历指定范围内的每个数,内层循环则找出该数的所有因子并求和。当因子之和等于该数时,则输出该数及其因子。
6670

被折叠的 条评论
为什么被折叠?



