【谭浩强课后习题】想用数组解决这类问题
原题是:9.一个数如果恰好等于它的因子之和,这这个数就称为“完数”。例如:
6=1+2+3,因此6是“完数”。编程序,找出1000之内的所有完数,
并按下面格式输出其因子:6 its factors are 1 2 3
我写的程序:
#include<stdio.h>
int main(void)
{
int a=1,b,c=1000,d,yin[];
if(a<=c)
{
a++;
for(b=1;b<a-1;b++)
{
d=a%b;
if(d==0)
{
yin[]=b;
}
}
}
printf("%d its factors are %d\n",a,yin[]);
return 0;
}
请问各位神仙,我该如何改进才可以实现这功能呢? #include <stdio.h>
int main()
{
int num = 2;
int n = 0;
int i = 0;
int j = 0;
int sum = 0;
int arr = {0};
for(i=1 ; i<1000 ; i++)
{
n = i;
num = 1;
sum = 0;
j = 0;
while(n/2 >= num)
{
if(n%num == 0)
{
sum += num;
arr = num;
}
num ++;
}
if(n == sum)
{
printf("%d its factors are ",n);
for(num = 0; num < j ; num ++)
{
printf("%d ",arr);
}
printf("\n");
}
}
return 0;
}
页:
[1]