码迷,mamicode.com
首页 > 其他好文 > 详细

打印出100到999的水仙花数

时间:2016-05-18 23:42:35      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如: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 }

 运行结果为:

技术分享

打印出100到999的水仙花数

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!