2029:【例4.15】水仙花数

 分析: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;
}

猜你喜欢

转载自blog.csdn.net/LWX3289765163/article/details/121340145