就按照水仙花的定义做就行。
#include <
stdio.h>
#include <math.h>
int main(void)
{
for(int i = 1000; i < 10000; i++)
{
int a = i / 1000;
int b = (i - a * 1000) / 100;
int c = (i % 100) / 10;
int d = i % 10;
if(pow(a, 4) + pow(b, 4) + pow(c, 4) + pow(d, 4) == i)
printf("%d\n", i);
}
return 0;
}
其中pow函数返回的double值可以直接和i做比较,因为本身就没有小数的问题。