关于✀完数✀的c语言编程问题。

2025-03-30 08:42:37
推荐回答(3个)
回答1:

#include
void main()
{
int i,s,t;
printf("===1000以内所有的完数===\n");
for (i=1;i<=1000;i++)
{
t=0;
for (s=1;s {
if (i%s==0)
t+=s;
}
if (t==i)
printf("%d,",i);
}
printf("\b \n");
}
这个是整体的思路,而格式的化你自己来设计吧!

回答2:

#include

void main()
{
int a,i,s=0;
for (a=1; a<=1000; a++)
{
s = 0;
for (i=1; i<=a/2; i++)
{
if (a%i == 0)
s = s+i;
}

if (s == a)
{
printf("%d its factors are ", a);

for (i=1; i<=a/2; i++)
{
if (a%i == 0)
printf("%d ", i);
}
printf("\n");
}
}
}

总共有3个
6
28
496

回答3:

# include
# include

int main() {
unsigned long int perfect_number = 2;
int m;

while (true) {
int n = static_cast(std::sqrt(perfect_number));
unsigned long int sum = 1;

for (int i=2; i<=n; i++) {
if (!(perfect_number % i)) {
m = perfect_number / i;
sum += m+i;

if (sum>perfect_number)
break;
}
}
if (sum == perfect_number)
std::cout<
perfect_number++;
}

return 0;
}