水仙花数

  水仙花数(Narcissistic number)是自幂数的一种,严格来说3位数的3次幂数才称为水仙花数。例如:1^3 + 5^3+ 3^3 = 153。

  接下来通过代码求100-999之间的水仙花数。 

package com.itszt.test8;
/**
 * 水仙花数
 */
public class Test4 {
    public static void main(String[] args) {
        //百位,十位,个位上的数
        int a=0,b=0,c=0;
        System.out.println("水仙花数有:");
        //100~1000之间的水仙花数
        for(int i=100;i<=1000;i++){
            a=i/100;//百位数
            b=(i%100)/10;//十位数
            c=(i%100)%10;//个位数
            a=a*a*a;
            b=b*b*b;
            c=c*c*c;
            if(a+b+c==i){
                System.out.println("  "+i);
            }
        }
    }
}

  上述代码运行结果如下: 

水仙花数有:
153
370
371
407
posted @ 2017-12-25 21:51  奔跑在梦想的道路上  阅读(367)  评论(0)    收藏  举报