如果一个 3 位数等于其各位数字的立方和,则称这个数为水仙花数。

如果一个 3 位数等于其各位数字的立方和,则称这个数为水仙花数。
例如:153 = 1^3 + 5^3 + 3^3,因此 153 就是一个水仙花数
请按照从小到大的顺序输出 1000 以内的水仙花数(3位数),并用"逗号"分隔输出结果

for i in range(100, 1000):
    res = sum(map(lambda x: pow(x, 3), map(int, str(i))))
    if res == i:
        print(i, end = ',')
'''--result
153,370,371,407,
'''

思路如下:
先写一个函数验证水仙花数,再用for循环找满足条件的数:

#include<iostream>
#include<cmath>
using namespace std;
bool isshuixianhuashu(int x) 
{
    int a=x/10%10;
    int b=x/100;
    int c=x%10;
    if(a*a*a+b*b*b+c*c*c==x)
        return true;
    else
        return false;
} 
int main()
{
    for(int i=2;i<1000;i++)
        if(isshuixianhuashu(i)==true)
            cout<<i<<endl;
    return 0;
}
 

OK,望采纳