/**
* @author cherhio
* @date Created in 2019-05-05 10:44
*
*
* 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
* 例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
*/
public class NarcissisticNumber {
public static void main(String[] args) {
for (int i = 100; i <999 ; i++) {
if (isNarcissisticNumber(i) == true){
System.out.println(i);
}
}
}
public static boolean isNarcissisticNumber(int i){
int hundred = i / 100;
int ten = i / 10 % 10;
int individual = i % 10;
if (Math.pow(individual,3) + Math.pow(ten,3) + Math.pow(hundred,3) == i){
return true;
}
return false;
}
}
输出结果:
153
370
371
407