#include <stdio.h>
int narcissistic(int number);
void PrintN(int m, int n);
int main()
{
int m, n;
scanf_s("%d %d", &m, &n);
if (narcissistic(m)) printf("%d is a narcissistic number\n", m);
PrintN(m, n);
if (narcissistic(n)) printf("%d is a narcissistic number\n", n);
return 0;
}
//你的代码将被嵌在这里
int narcissistic(int number)
{
int a, b, c;
a = number / 100;
b = (number - a * 100) / 10;
c = number % 10;
if (a * a * a + b * b * b + c * c * c == number)
return(1);
else
return(0);
}
void PrintN(int m, int n)
{
for (m = m + 1; m < n; m++)
if (narcissistic(m))
printf("%d\n", m);
}
函数narcissistic
判断number
是否为水仙花数,是则返回1,否则返回0。
函数PrintN
则打印开区间(m
, n
)内所有的水仙花数,每个数字占一行。题目保证100≤m
≤n
≤10000。