输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如,153是一水仙花数,因为153=13+53+33。
这里使用函数调用的方法:
算法思想:
1.取该数据的各个位将他们三次方表示出来并相加
while (a != 0)
{
temp = a % 10;//个位
a = a / 10;
sum += temp * temp * temp;
}
2.判断计算之后的数据和原数据是否一致
if (n == sum)
return true;
else
return false;
//if-else可以更换为return sum==n;
3.函数在主函数中调用使用
int main()
{
bool x=isflower(153);
printf("%d", x);
}
详细代码如下所示:
#include <stdio.h>
bool isflower(int n)
{
int temp = 0, a = n, sum = 0;
while (a != 0)
{
temp = a % 10;//个位
a = a / 10;
sum += temp * temp * temp;
}
if (n == sum)
return true;
else
return false;//if-else可以更换为return sum==n;
}
int main()
{
bool x=isflower(153);
printf("%d", x);
}