打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
分析:1."水仙花数"是一个三位数,可将此数定义为num;
2.其中每各个位上的数字的立方和等于该数的本身;
将这三位数定义为a,b,c
其中a表示个位数;b表示十位数;c表示百位数
public class Test {
public static void main(String[] args) {
for (int i = 100; i < 999; i++) {
if (isShuiXianHua(i)) {
System.out.println(i);
}
}
}
public static boolean isShuiXianHua(int num) {
if (num <100 || num >999) {
return false;
}else{
int a = num % 10;
int b = (num / 10)%10;
int c = num / 100;
int total = a * a * a + b * b * b + c * c * c ;
if (total == num) {
return true;
}else{
return false;
}
}
}
}
运行结果: