打印出100到999的水仙花數


所謂"水仙花數"是指一個三位數,其各位數字立方和等於該數本身。例如: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 }

 運行結果為:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM