要解决这个问题,首先要弄明白水仙花数是什么?水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。
水仙花数只是自幂数的一种,严格来说3位数的3次幂数才称为水仙花数。
附:其他位数的自幂数名字;一位自幂数:独身数;两位自幂数:没有;三位自幂数:水仙花数;四位自幂数:四叶玫瑰数;五位自幂数:五角星数;六位自幂数:六合数;七位自幂数:北斗七星数;八位自幂数:八仙数;九位自幂数:九九重阳数;十位自幂数:十全十美数
重点就是三位数,直接上代码
#include <stdio.h>
#include <stdlib.h>
int main() {
int m, n;
printf("请输入区间:");
scanf("%d%d", &m, &n);
int i, a, b, c;
for (i = m; i <= n; i++) {
a = i / 100;
b = i / 10 % 10;
c = i % 10;
if (i == a * a * a + b * b * b + c * c * c && i != 1 && i != 1000) {
printf("水仙花数为:%d\n", i);
}
}
return i;
}
运行结果如下
注意:1和1000不是水仙花数