水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。
public class Narcissistic_number {
public static void main(String[] args) {
int ge, shi, bai;
for (int i = 100; i <= 999; i++) {//水仙花数范围
ge = i % 10;//获取个位的数字
shi = i / 10 % 10;//获取十位的数字
bai = i / 100;//获取百位的数字
//水仙数的判断,个十百位上数的三次方和为原数
//Math.pow(a,b)即a的b次方
if ((Math.pow(ge, 3) + Math.pow(shi, 3) + Math.pow(bai, 3)) == i) {
System.out.println(i);//打印出水仙花数
}
}
}
}