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

水仙花数

时间:2017-10-23 23:07:33      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:class   a*   bsp   取出   static   while   pre   等于   demo   

水仙花数,又称为阿姆斯特朗数,水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。

使用Java编写一个水仙花数且 100 < i < 1000。主要思想,要明白如何取出水仙花数 i 的个位,十位,百位。

 

class Demo{
    public static void main(String[] args){
        
        int i = 100;
        
        while(i < 1000){
            //获得个位
            int a = i % 10;
                        //获得十位
            int b = (i % 100) / 10;
                        //获得百位
            int c = i /100;
                        //判断是否为水仙花数
            if( i == a*a*a + b*b*b + c*c*c){
                System.out.println(i);
                }
                i++;
            }
    
    }    
}

 

水仙花数

标签:class   a*   bsp   取出   static   while   pre   等于   demo   

原文地址:http://www.cnblogs.com/chenttc/p/7719436.html

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