试题系列二(求1000内的完数)
1.求1000内的完数。(所有因子之和等于本身的数位完数,数本身不算因子)
1 #include<stdio.h> 2 3 void perfectNum(int num); 4 5 int main(int argc, char** argv) 6 { 7 int num = 0; 8 9 printf("please input a number:"); 10 scanf("%d", &num); 11 12 perfectNum(num); 13 14 return 0; 15 } 16 17 void perfectNum(int num) 18 { 19 int i = 0; 20 int j = 0; 21 int factor = 0; 22 23 for (i = 1; i <= num; i++) 24 { 25 factor = 0; 26 27 for (j = 1; j <= i/2; j++) 28 { 29 if (0 == i%j) 30 factor += j; 31 } 32 33 if (factor == i) 34 { 35 printf("%d is perfect number\n", i); 36 } 37 } 38 }