题目
输出所有的“水仙花数”,所谓的“水仙花数”是指一个三位数其各位数字的立方和等于该数本身,例如153是“水仙花数”,因为:153 = 13 + 53 + 33。
详解
#include<stdio.h>
int main() {
//水仙花数
//从100开始进行判断,到999结束
for (int num = 100; num < 1000; num++) {
//数学方法获取个十百位上的数字
int f = num / 100;
int s = (num - f * 100) / 10 ;
int t = num % 10;
//判断
if (num == f * f * f + s * s * s + t * t * t) {
//输出
printf("水仙花数为 %d \n", num);
}
}
printf("\n");
return 0;
}