1 #include <stdio.h> 2 3 int factorsum(int number); 4 void PrintPN(int m, int n); 5 6 int main() 7 { 8 int i, m, n; 9 10 scanf_s("%d %d", &m, &n); 11 if (factorsum(m) == m) printf("%d is a perfect number\n", m); 12 if (factorsum(n) == n) printf("%d is a perfect number\n", n); 13 PrintPN(m, n); 14 15 return 0; 16 } 17 18 /* 你的代码将被嵌在这里 */ 19 int factorsum(int number) 20 { 21 int sum = 0; 22 if (number == 1) 23 { 24 return 1; 25 } 26 for (int i = 1; i < number; i++) 27 { 28 if (number%i==0) 29 { 30 sum = sum + i; 31 } 32 } 33 34 return sum; 35 } 36 37 void PrintPN(int m, int n) 38 { 39 int is_factorsum = 0; 40 int j; 41 for (int i = m; i <=n; i++) 42 { 43 if (factorsum(i) == i) 44 { 45 printf("%d = 1", i); 46 for (j = 2; j < i; j++) 47 { 48 if (i % j == 0) 49 { 50 printf(" + %d", j); 51 } 52 } 53 printf("\n"); 54 is_factorsum = 1; 55 } 56 } 57 if (is_factorsum == 0) 58 { 59 printf("No perfect number\n"); 60 } 61 }
原文地址:https://www.cnblogs.com/2018jason/p/10962314.html
时间: 2024-10-15 19:11:50