PTA输出指定范围内的完数
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("%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 int factorsum(int number) 19 { 20 int sum=0; 21 if(number==1) 22 return 1; 23 for(int i=1;i<number;i++) 24 { 25 if(number%i==0) 26 { 27 sum+=i; 28 } 29 } 30 if(sum==number) 31 return sum; 32 } 33 34 void PrintPN(int m,int n) 35 { 36 int exist=0,isfactor=0; 37 for(int j=m;j<=n;j++) 38 { 39 if(factorsum(j)==j) 40 { 41 exist=1; 42 printf("%d =",j); 43 if(j==1) 44 { 45 printf(" %d",j); 46 } 47 for(int k=1;k<j;k++) //此时j为完数 48 { 49 if(j%k==0) 50 { 51 if(k==1) 52 printf(" 1"); 53 else 54 printf(" + %d",k); 55 } 56 } 57 printf("\n"); 58 } 59 } 60 if(exist==0) 61 { 62 printf("No perfect number"); 63 } 64 }