Java判断水仙花数

水仙花数

水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。

// 取个位、十位、百位、千位依次这样计算:n/1%10,n/10%10,n/100%10,n/1000%10
public class Test {
	public static void main(String[] args) {
		int i, j, k, l;
		System.out.println("100-999之间的水仙花数有:");
		for (i = 100; i <= 999; i++){
			j = i / 1 % 10;  //个位
			k = i / 10 % 10;  //十位 
			l = i / 100 % 10;  //百位 
			
			if (i == Math.pow(j,3) + Math.pow(k,3) + Math.pow(l,3)){  // Math.pow(double number, double n)返回一个数的n次幂 
				System.out.println(i);
			}
		}
	}
}

执行结果:
	100-999之间的水仙花数有:
	153  // 153 = 1 + 125 + 27
	370
	371
	407
posted @ 2019-02-09 19:13  每一天,为明天。  阅读(371)  评论(0)    收藏  举报