0
点赞
收藏
分享

微信扫一扫

水仙花数(do-while循环)

_LEON_ 2022-01-21 阅读 38

【题目要求】

水仙花数是指一个3位数,它的各位数的3次方和为本身(153=1^3+5^3+3^3)。请输出所有3位数中的水仙花数。

【题目分析】

1.循环遍历所有3位数

2.判断是否为水仙花数

#include<iostream>
using namespace std;
int main()
{
	int n=100;
	do
	{
		int a=0,b=0,c=0;
		a=n%10;  //个位
		b=n/10%10;  //n/10用于舍弃掉个位数
		c=n/100;  //舍弃掉后两位即可得百位
		if(a*a*a+b*b*b+c*c*c==n)
		{
			cout<<n<<endl;
		 } 
		n++;
	}while(n<1000);
	return 0;
}
举报

相关推荐

0 条评论