分析:1.循环遍历数字;2.取出各位数字;3.判断是否符合条件。
#include <iostream>
using namespace std;
int main()
{
//100到999的水仙花数
int p1, p2, p3;//记录个、十、百位
for (int i = 100; i <= 999; ++i)
{
p3 = i / 100;
p2 = (i / 10) % 10;
p1 = i - 100 * p3 - 10 * p2;
if (p1 * p1 * p1 + p2 * p2 * p2 + p3 * p3 * p3 == i)
{
cout << i << endl;
}
}
}
同理(未曾设想的道路):
#include<iostream>
using namespace std;
int main(){
cout<<"153"<<endl;
cout<<"370"<<endl;
cout<<"371"<<endl;
cout<<"407"<<endl;
}