java求1000以内的水仙花数

 

水仙花数是指一个 n 位数 ( n>=3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)

 

  三位的水仙花数共有4个,分别为:153、370、371、407

代码实现:

 

public class For_Demo2 {
		public static void main(String[] args) {
			//求水仙花数
			int ge,shi,bai;
			int m=0;
			int total=0;
			for(int i=100;i<1000;i++){
				ge=i%10;
				shi=i/10%10;
				bai=i/100;
				int b=(int) (Math.pow(ge,3)+Math.pow(shi, 3)+Math.pow(bai, 3));
				if(i==b){	
					System.out.println(i);
					total+=i;
					m++;	
				}
			}
			System.out.println("total:"+total);
			System.out.println(m);
		}
}

注意:取出每位的值。
 

 

 

posted @ 2017-08-29 21:15  杰醍奋  阅读(276)  评论(0编辑  收藏  举报