题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数 本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
代码:
#include <stdio.h>
#include <math.h>
/*
题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数
本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
*/
int main()
{
//筛选方法:穷尽枚举
int a;//百位数
int b;//十位数
int c;//个位数
printf("所有水仙花数:");
for (int num = 100; num < 1000; num++)
{
a = num / 100;
b = num % 100 / 10;
c = num % 10;
if (num == pow(a, 3) + pow(b, 3) + pow(c, 3))//pow(x,3):x的3次方
{
printf("%d ", num);
}
}
return 0;
}
运行结果: