所謂"水仙花數"是指一個三位數,其各位數字立方和等於該數本身。例如:153是一個水仙花數,因為153=1*1*1 + 5*5*5 + 3*3*3
1 public class Flower 2 { 3 public static void main(String[] args) 4 { 5 int temp=0; 6 System.out.println("水仙花數為:"); 7 for (int i=100;i<999 ;i++ ) 8 { 9 temp = i; 10 int x= temp/100;//算出百位數, 因為x是int類型 所以小數省去 11 int y= temp%100/10;//算出十位數 12 int z= temp%10;//算出個位數 13 if (i==x*x*x+y*y*y+z*z*z) 14 { 15 System.out.println(i); 16 } 17 } 18 } 19 }
運行結果為: