水仙花数也被称为超完全数字不变数,水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身。例如:1^3 + 5^3+ 3^3 = 153。
public class NarcissisticNumber {
public static void main(String[] args){
//分析:
//1、int n
//2、先求n的百位十位和个位的数
//百位:n / 100; 十位: n % 100 / 10; 个位:n % 10
int n = 153;
int hundredDit = n / 100;
int ten = n % 100 / 10;
int bit = n % 10;
if (n == hundredDit * hundredDit * hundredDit + ten * ten * ten + bit * bit * bit) {
System.out.println(n + "是水仙花数");
}else{
System.out.println(n + "不是水仙花数");
}
}
}