C语言实现完数判断 - 找出1000以内的所有完数
#include <stdio.h>
int wanshu(int n) { int x = 0, i; for(i = 1; i <= n/2; i++) { if(n % i == 0) //判断i是否为n的因子 { x = x + i; //累加因子 } } if(x == n) //判断是否为完数 { return 1; } else { return 0; } }
int main() { int n; for(n = 1; n <= 1000; n++) { if(wanshu(n) == 1) { printf('%d ', n); } } return 0; }
AI 优化版
原文地址:https://www.cveoy.top/t/topic/nVL0 著作权归作者所有。请勿转载和采集!