java 水仙花數 提升檔次解法

輸出所有的水仙花數,把謂水仙花數是指一個數3位數,其各各位數字立方和等於其本身,
例如: 153 = 1*1*1 + 3*3*3 + 5*5*5

// 六行代碼解決 

public static void main(String[] args) throws ParseException {

for(int a = 100;a<1000;a++) {
int i = (int) Math.ceil(a/100);
int j = (int) Math.ceil(((int) a%100)/10);
int k = a%100%10;
if (a == Math.pow(i, 3) + Math.pow(j, 3) + Math.pow(k, 3)) {
System.out.println(a);
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章