水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。
package narcissistic.number;
/**
*
* @SkyBlueLiu 求水仙花数问题
*
*/
public class NarcissisticNumber {
public static void main(String[] args) {
System.out.println("输出三位数的水仙花数");
int temp=0;
for(int x=100;x<1000;x++) {
int a=x%10; //a是个位数
int b=x/10%10; //b是十位数
int c=x/10/10%10; //c是百位数
if(x==a*a*a+b*b*b+c*c*c) {
temp++;
System.out.println("三位水仙花数有:"+x);
}
}
System.out.println("共有水仙花数:"+temp);
}
}
测试: