user-0 提问: 代码优化

回复:
user: user-1
content: 要那样输出的话可以参考这个：  #include <stdio.h> #include <math.h> int factorsum(int n, int* fac)  //除了计算因子和，还在数组fac里给出因子列表，以-1表示结束 { 	int i, sum = 1, k, *p = fac + 1, half[40], j = 0; 	if (n == 1)return 0; 	for (i = 2; i <= (int)(sqrt(n) + 0.001); i++)  //从2到根号n测试 	{ 		if (n%i == 0)  //每次整除产生两个因子 		{ 			sum += (*p++ = i); //小的那个直接升序记入数组 			if (i < (k=n/i))  //除非 i==k 这次只产生1个因子 				sum += (half[j++] = k);  //大的那个降序另存 		} 	} 	while (--j >= 0) //把较大的一半因子逆序抄过去 	{ 		*p++ = half[j]; 	} 	*p = -1;  //表示结束 	return sum; } void PrintPN(int m, int n) { 	int i, fac[70] = { 1 }; 	for (i = m; i <= n; i++) 	{ 		if (i == factorsum(i, fac)) 		{ 			int *p = fac + 1; 			printf("%d=1", i); 			while (*p > 0) 			{ 				printf("+%d", *p++); 			} 			printf("\n"); 		} 	} } int main() { 	int m, n; 	scanf("%d%d", &m, &n); 	PrintPN(m, n); 	return 0; }

user: user-2
content: 判断是否为完数的函数再优化一点点：int factorsum( int n)//判断是否为完数 {     int k=2,s=1;     while(k <= n/2){          if(n%k==0)s+=k;          k++;     }     if(s==n && n!=1) return 1;     return 0; }

user: user-3
content: #include <stdio.h>  int factorsum( int number ); void PrintPN( int m, int n );  static void swap(int *a, int *b); static void print_perfect(int m, int n);  int main() {     int m, n;      scanf("%d %d", &m, &n);      if (m > n) {         swap(&m, &n);     }      print_perfect(m, n);     /*     if ( factorsum(m) == m ) printf("%d is a perfect number\n", m);     if ( factorsum(n) == n ) printf("%d is a perfect number\n", n);     PrintPN(m, n);     */     return 0; }  static void swap(int *a, int *b) {     int tmp;      tmp = *a;     *a = *b;     *b = tmp; }  static void print_perfect(int m, int n) {     int i;      for (i = m; i < n; i++) {         if (i == 1)             continue;         if (factorsum(i)) {             printf("%d is a perfect number!\n", i);         }     } }   int factorsum( int number )  //判断是否为完数 {     int i;     int sum = 1;       for (i = 2; i < number / 2 + 1; i++) {         if (number % i == 0)             sum += i;     }     if (sum == number)         return 1;      return 0;     /*     int i,j;     int m = 1;     int n,num = 0;     int a[100] = {0};     a[0] = 1;     if(number == 0)         return -1;     if(number == 1)         return 1;     for(i = 2; i < number; i ++ )     {         for(j = i + 1; j < number; j ++)         {             if(i * j == number)             {                 a[m] = i;                 m ++;                 a[m] = j;                 m ++;             }          }     }     for(n = 0; n < 100; n ++)     {         if(a[n] == 0)             break;         num = num + a[n];     }     if(num == number)         return num;     else         return 0;     */ }  void PrintPN( int m, int n )   //找出m到n之间的完数 {     int q;     int i,j;     int num = 0;      for(int k = m; k <= n; k++)     {         if(factorsum(k) == k)         {             num = 1;             int p = 1;             int a[100] = {0};             a[0] = 1;             for(i = 2; i < k; i ++ )             {                 for(j = i + 1; j < k; j ++)                 {                     if(i * j == k)                     {                         a[p] = i;                         p ++;                         a[p] = j;                         p ++;                      }                 }             }             //排序             int b;             for(b = 0; a[b] != 0; b++)             {                 for(int c = b + 1; a[c] != 0; c ++)                 {                     if(a[b] > a[c])                     {                         int key;                         key = a[b];                         a[b] = a[c];                         a[c] = key;                     }                  }              }             printf("%d = ",k);             for( q = 0; q < 100; q ++)             {                 if(a[q] == 0)                 {                     printf("\n");                     break;                 }                 if(q == 0)                     printf("%d",a[q]);                 else                     printf(" + %d",a[q]);              }         }     }     if(num == 0)         printf("No perfect number\n");  } 供参考～

user: user-2
content: 修正楼上第10行：for(int i=m;i<=n;i++){

user: user-2
content: 楼主考虑的太复杂了，供参考：#include <stdio.h> int factorsum( int n );//判断是否为完数  int main() {     int m, n;     scanf("%d %d", &m, &n);     if(m<=0 || n>10000) return -1;      for(int i=m;i<n;i++){        if(factorsum(i))printf("%d is a perfect number\n", i);     }          return 0; }   int factorsum( int n) {     int k=1,s=0;     while(k <= n/2){          if(n%k==0) s+=k;          k++;     }     if(s==n) return 1;     return 0; }

user: user-4
content: 随便搜个对正整数分解因数的代码