```c
#include <stdio.h>
#include <math.h>
int main()
{
int i = 0;
for (i = 100; i < 100000; i++)
{
if (i >= 100 && i < 1000 && (pow(i / 100, 3) + pow(i / 10 % 10, 3) + pow(i % 100, 3)) == i)
{
printf("%d ", i);
}
if (i >= 1000 && i < 10000 && (pow(i / 1000, 4) + pow(i / 100 % 10, 4) + pow(i % 100 / 10, 4) + pow(i % 1000, 4)) == i)
{
printf("%d ", i);
}
if (i >= 10000 && i < 100000 && (pow(i / 10000, 5) + pow(i / 1000 % 10, 5) + pow(i / 100 % 100, 5) + pow(i % 100 / 100, 5) + pow(i % 10000, 5)) == i)
{
printf("%d ", i);
}
}
return 0;
}
```
#include <stdio.h>
#include <math.h>
int is_narcissistic_number(int x)
{
int n = (int)log10(x) + 1;
int a = x;
int s = 0;
while (a)
{
s += pow(a % 10, n);
a /= 10;
}
return s == x;
}
int main()
{
for (int i = 100; i < 100000; i++)
{
if (is_narcissistic_number(i))
printf("%d ", i);
}
return 0;
}
修改如下,供参考:
#include <stdio.h>
#include <math.h>
int main()
{
int i = 0;
for (i = 100; i < 100000; i++)
{
if (i >= 100 && i < 1000 && (pow(i / 100, 3) + pow(i / 10 % 10, 3) + pow(i % 10, 3)) == i)
//if (i >= 100 && i < 1000 && (pow(i / 100, 3) + pow(i / 10 % 10, 3) + pow(i % 100, 3)) == i)
{
printf("%d ", i);
}
if (i >= 1000 && i < 10000 && (pow(i / 1000, 4) + pow(i / 100 % 10, 4) + pow(i / 10 % 10, 4) + pow(i % 10, 4)) == i)
//if (i >= 1000 && i < 10000 && (pow(i / 1000, 4) + pow(i / 100 % 10, 4) + pow(i % 100 / 10, 4) + pow(i % 1000, 4)) == i)
{
printf("%d ", i);
}
if (i >= 10000 && i < 100000 && (pow(i / 10000, 5) + pow(i / 1000 % 10, 5) + pow(i / 100 % 10, 5) + pow(i / 10 % 10, 5) + pow(i % 10, 5)) == i)
// if (i >= 10000 && i < 100000 && (pow(i / 10000, 5) + pow(i / 1000 % 10, 5) + pow(i / 100 % 100, 5) + pow(i % 100 / 100, 5) + pow(i % 10000, 5)) == i)
{
printf("%d ", i);
}
}
return 0;
}