详解在代码中已标示,数据范围可更改
//自己摸索的水仙花数(φ(◎ロ◎;)φ)
#include<tgmath.h>
#include<math.h>
int main()
{
int sum = 0;
int i = 0;
//floor为向下取整符号,对数函数算出的指数即位数减一 头文件#include<tgmath.h>
for (i = 1;i <= 1000;i++)
{
int sum = 0;
int j = floor(log10(i) + 1);
int a = floor(log10(i) + 1);
int b = i;
//double pow(x,y)表示x的y次方 #include<math.h>(int)为强制取整
while(j > 0)
{
int k = b / pow(10,j-1);
sum += pow(k, a);
b = b % (int)pow(10, j - 1);
j--;
}
if (i == sum)
{
printf("%d ", i);
}
}
return 0;
}
标签:10,探索,floor,int,pow,sum,打印,include,水仙花 From: https://blog.51cto.com/u_16298007/8155560