打印出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 }

 运行结果为:

原文地址:https://www.cnblogs.com/coolso/p/5506985.html