题目:打印出所有的水仙花数 。所谓水仙花数是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 水仙花数 ,因为153=1的三次方+5的三次方+3的三次方。
题目简单,直接上代码:
public ArrayList<Integer> getSXHS() {
// TODO Auto-generated method stub
ArrayList<Integer> sxhs = new ArrayList<Integer>();
for(int i = 100; i <= 999; i++){
int i1 = i / 100;
int temp = i % 100;
int i2 = temp / 10;
int i3 = temp % 10;
int iresult = (int) (Math.pow(i1, 3) + Math.pow(i2, 3) + Math.pow(i3, 3));
if(i == iresult) {
sxhs.add(new Integer(i));
}
}
return sxhs;
}