1 // Test1016-因子之和.cpp: 定义控制台应用程序的入口点。 2 // 3 #include "stdafx.h" 4 5 int factor[100] = {}; 6 int index = 0; 7 int isPerfect(int num) 8 { 9 int equal = 0; 10 index = 0; 11 for (int i = 1; i <= num / 2+1; i++) 12 { 13 if (num % i == 0) 14 { 15 equal += i; 16 factor[index++] = i; 17 } 18 } 19 if (equal == num) 20 return 1; 21 else 22 return 0; 23 } 24 int main() 25 { 26 int num; 27 scanf_s("%d", &num); 28 29 for (int i = 2; i <= num; i++) 30 { 31 if (isPerfect(i) == 1) 32 { 33 printf("%d its factors are ", i); 34 for (int j = 0; j < index; j++) 35 printf("%d ", factor[j]); 36 printf(" "); 37 index = 0; 38 } 39 } 40 return 0; 41 }
结果
1 10000 2 6 its factors are 1 2 3 3 28 its factors are 1 2 4 7 14 4 496 its factors are 1 2 4 8 16 31 62 124 248 5 8128 its factors are 1 2 4 8 16 32 64 127 254 508 1016 2032 4064 6 请按任意键继续. . .