水仙花数
水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。
C++实现代码
#include <iostream>
#include <math.h>
using namespace std;
int f,s,t,temp;
int judge(int n){//拆数,针对三位数
f=n/100;
s=(n/10)%10;
t=n%10;
temp=pow(f,3)+pow(s,3)+pow(t,3);
if(temp==n){
return n;
}else{
return 0;
}
}
int main(){
int m,n,i,count=0;
cin>>m>>n;
for(i=m;i<=n;i++){
if(judge(i)){
cout<<judge(i)<<" ";
count++;
}
}
if(!count){
cout<<"no";//非水仙花数
}
}