水仙花数是一道比较简单的题目,即找出所有满足各位的立方之和为该三位数本身的三位数。
代码为:
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
int h, t, d;
for(int n = 100; n < 1000; n++)
{
h = n / 100;
t = n / 10 % 10;
d = n % 10;
if(n == (h * h * h + t * t * t + d * d * d))
printf("%d ", n);
}
return 0;
}
运行结果: