
水仙花数(Narcissistic number),又称阿姆斯特朗数(Armstrong number),是指一个三位数,其各位数字的3次幂之和等于该数本身。例如:153 = 1³ + 5³ + 3³。

public class Test {
public static void main(String[] args) {
for (int i = 0; i < 10000; i++) {
//计算i是几位数
int count = 0;
int sum = 0;
int tmp = i;
while(tmp != 0){
tmp /= 10;
count ++;
}
tmp = i;
//求和
while(tmp != 0){
sum += Math.pow(tmp % 10,count);//(tmp%10)的count次方
tmp /= 10;
}
//判断
if(sum == i){
System.out.println(sum);
}
}
}
}
for (int i = 0; i < 10000; i++) {
}遍历1~9999的所有整数
int count = 0;
int sum = 0;
int tmp = i;
while(tmp != 0){
tmp /= 10;
count ++;
}
//求和
while(tmp != 0){
sum += Math.pow(tmp % 10,count);//(tmp%10)的count次方
tmp /= 10;
}Math.pow(a,b)------a的b次方
//判断
if(sum == i){
System.out.println(sum);
}如果计算出来的数和i相同,就是水仙花数
【总结】
通过本文,我们不仅了解了水仙花数这一有趣的数字概念,还通过Java代码实现了找出10000以内的水仙花数。这一过程不仅加深了我们对数字特性的理解,也提升了我们的编程能力。希望大家在今后的编程学习中,能够继续探索更多有趣的数字和算法,享受编程带来的乐趣。