水仙花数的求解

  所谓水仙花数是指数字各位数的位长次幂之和等于它本身,举例说明:153,是一个三位数,位长为3,各个位上的数字分别为1,5,3,则13+53+33=153,等于他本身,则153是水仙花数。

  编程求解所有三位数的水仙花数

 1 public class Daffnum {
 2     public static void main(String[] args) {
 3         // TODO Auto-generated method stub
 4         for (int i = 100; i < 1000; i++) {
 5             int h=i/100%10;//get hundreds place number
 6             int t=i/10%10;
 7             int g=i%10;
 8             //int s=h*h*h+t*t*t+g*g*g;
 9             int s=(int)(Math.pow(h,3)+Math.pow(t,3)+Math.pow(g,3));
10             if (s==i) {
11                 System.out.println(s);
12             }
13         }
14     }
15 }

 

 

posted @ 2013-11-26 13:25  sdrzlyz  阅读(231)  评论(0编辑  收藏  举报