user-0 提问: 求大佬解答

回复:
user: user-1
content: #include <stdio.h>   #define MAX_SIZE        128 #define MAX_NUM         1000  int is_perfect_number(int num, int *pfactor_list, int list_len, int *pcnt);  int main(void) {     int factor_list[MAX_SIZE], cnt;     int i, j;      for (i = 1; i < MAX_NUM; i++) {         if (i % 10 != 6 && i % 10 != 8)    //完全数都是以6或8结尾             continue;         if (is_perfect_number(i, factor_list, MAX_SIZE, &cnt)) {             printf("%d its factors are ", i);             for (j = 0; j < cnt - 1; j++)                 printf("%d,", factor_list[j]);             printf("%d\n", factor_list[j]);         }     }      return 0; }  int is_perfect_number(int num, int *pfactor_list, int list_len, int *pcnt) {     int i, j;     int sum = 0;      j = 0;     for (i = 1; i <= num / 2; i++)         if (num % i == 0) {             pfactor_list[j++] = i;             sum += i;             if (j >= list_len)                 break;         }      *pcnt = j;     if (sum == num)         return 1;      return 0; } 增加代码优化，供参考～

user: user-1
content: #include <stdio.h>   int check_perfect_number(int num, int *pfactor_list, int *pcnt);  int main(void) {     int factor_list[128], cnt;     int i, j;      for (i = 1; i < 10000; i++) {         if (check_perfect_number(i, factor_list, &cnt)) {             printf("%d its factors are ", i);             for (j = 0; j < cnt - 1; j++)                 printf("%d,", factor_list[j]);             printf("%d\n", factor_list[j]);         }     }      return 0; }  int check_perfect_number(int num, int *pfactor_list, int *pcnt) {     int i, j;     int sum = 0;      j = 0;     for (i = 1; i <= num / 2; i++)         if (num % i == 0) {             pfactor_list[j++] = i;             sum += i;         }      *pcnt = j;     if (sum == num)         return 1;      return 0; } 供参考～

user: user-2
content: https://jingyan.baidu.com/article/3065b3b65e9060becff8a4ff.html

user: user-3
content: 引用 3 楼 user-4的回复:[quote=引用 1 楼 qq_45798109的回复:]自己改一下吧 #include <iostream> using namespace std;  int main() {   int m, n, count = 0;   cin >> m >> n;   for (int i = m; i <= n; ++i) {     int a[10000] = { 0 }, index = 0, sum = 0;     for (int j = 1; j < i; ++j) {       if (i % j == 0) {         a[index++] = j;         sum += j;       }     }     if (sum == i) {       count++;       cout << i << " = " << a[0];       for (int k = 1; k < index; ++k)         cout << " + " << a[k];       cout << endl;     }   }   if (!count) cout << "None";   return 0; } 皇上要登基了，你写的这么复杂[/quote] 这是我之前写的😂，确实很复杂

user: user-4
content: 引用 1 楼 qq_45798109的回复:自己改一下吧 #include <iostream> using namespace std;  int main() {   int m, n, count = 0;   cin >> m >> n;   for (int i = m; i <= n; ++i) {     int a[10000] = { 0 }, index = 0, sum = 0;     for (int j = 1; j < i; ++j) {       if (i % j == 0) {         a[index++] = j;         sum += j;       }     }     if (sum == i) {       count++;       cout << i << " = " << a[0];       for (int k = 1; k < index; ++k)         cout << " + " << a[k];       cout << endl;     }   }   if (!count) cout << "None";   return 0; } 皇上要登基了，你写的这么复杂

user: user-0
content: 引用 1 楼 qq_45798109的回复:自己改一下吧 #include <iostream> using namespace std;  int main() {   int m, n, count = 0;   cin >> m >> n;   for (int i = m; i <= n; ++i) {     int a[10000] = { 0 }, index = 0, sum = 0;     for (int j = 1; j < i; ++j) {       if (i % j == 0) {         a[index++] = j;         sum += j;       }     }     if (sum == i) {       count++;       cout << i << " = " << a[0];       for (int k = 1; k < index; ++k)         cout << " + " << a[k];       cout << endl;     }   }   if (!count) cout << "None";   return 0; } 能用c语言写的简单一点吗，目前还只学到循环

user: user-3
content: 自己改一下吧 #include <iostream> using namespace std;  int main() {   int m, n, count = 0;   cin >> m >> n;   for (int i = m; i <= n; ++i) {     int a[10000] = { 0 }, index = 0, sum = 0;     for (int j = 1; j < i; ++j) {       if (i % j == 0) {         a[index++] = j;         sum += j;       }     }     if (sum == i) {       count++;       cout << i << " = " << a[0];       for (int k = 1; k < index; ++k)         cout << " + " << a[k];       cout << endl;     }   }   if (!count) cout << "None";   return 0; }