水仙花数判断原理图:
利用do…while()循环判断:
参考代码:
#include<iostream>
using namespace std;
int main(){
int num = 100;
do
{
//从所有三位数中找到水仙花数
int a = num%10; //取个位
int b = num/10%10; //取十位
int c = num/100; //取百位
if(a*a*a+b*b*b+c*c*c==num){
cout <<"水仙花数为:"<< num <<endl;
}
num++;
}while(num<1000);
return 0;
}
利用for()循环判断:
参考代码:
#include<iostream>
using namespace std;
int main(){
for( int num=100; num<1000; num++)
{
int a = num%10; //取个位
int b = num/10%10; //取十位
int c = num/100; //取百位
if(a*a*a+b*b*b+c*c*c==num){
cout <<"水仙花数为:"<< num <<endl;
}
}
return 0;
}