C语言实现三位水仙花数之和
#include <stdio.h> int sxh() { int x, n = 0; for(x=100;x<=999;x++) { int digit1 = x / 100; // 百位数字 int digit2 = (x % 100) / 10; // 十位数字 int digit3 = x % 10; // 个位数字
if(digit1*digit1*digit1 + digit2*digit2*digit2 + digit3*digit3*digit3 == x) // 判断是否为水仙花数
{
printf('%d ',x);
n += x;
}
}
return n;
}
int main() { printf("\n三位水仙花数之和为:\n%d\n",sxh()); return 0; }
原文地址: https://www.cveoy.top/t/topic/bpLc 著作权归作者所有。请勿转载和采集!