C语言实现水仙花数判断与累加 - 详解代码及优化
#include <stdio.h>
int sxh() { int x, n = 0;
for(x=100;x<=999;x++) { int temp = x; int sum = 0; while(temp > 0) { int digit = temp % 10; sum += digit * digit * digit; temp /= 10; }
if(sum == x) {
printf('%d ',x);
n += x;
}
}
return n; }
int main() { printf("\n三位水仙花数之和为:\n%d\n",sxh()); return 0; }
原文地址: https://www.cveoy.top/t/topic/bpMp 著作权归作者所有。请勿转载和采集!