/ 题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。
// 例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
// 例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
// 程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
package com.homework3;
public class Demo3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo3 d3 =new Demo3();
for(int i=100;i<=999;i++)
{
if(d3.judge(i)==true)
{
System.out.println(i);
}
}
}
public boolean judge(int n)
{
boolean b =false;
if(n<100||n>=1000)
{
System.out.println("error");
}
else
{
int x = n/100;
int y = (n%100)/10;
int z = n%10;
if(x*x*x+y*y*y+z*z*z==n)
{
b = true;
}
}
return b;
}
}