????????水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个数位上的数字的 3次幂之和等于它本身。例如:1^3 + 5^3+ 3^3 = 153。先来代码,后期给大家讲解:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int flower(int i)
{
int count = 0;
int temp = i;
int sum = 0;
while (temp)
{
temp /= 10;
count++;
}
temp = i;
while (temp)
{
sum += pow(temp % 10, count);
temp /= 10;
}
if (sum == i)
{
return 1;
}
else
return 0;
}
int main()
{
printf("你想打印从0-几的水仙花数字,请输入->\n");
int n;
scanf("%d", &n);
int i = 0;
for (i = 0; i <= n; i++)
{
if (flower(i))
{
printf("%d ", i);
}
}
return 0;
}