qfedu-c-level/day6/homework/h7.c

29 lines
726 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 编写程序并设计函数, 接收一个整数值,验证这个数值是否为水仙花数。 【提示】水仙花数每一位数字的3次幂累加的结果和这个数本身相同
#include <stdio.h>
#include <math.h>
int isNumberOfDaffodils(int);
int main()
{
int n;
printf("请输入一个三位整数: ");
scanf("%d", &n);
if (isNumberOfDaffodils(n))
printf("%d 是水仙花数", n);
else
printf("%d 不是水仙花数", n);
return 0;
}
int isNumberOfDaffodils(int num)
{
int i, j, k;
i = num / 100;
j = (num % 100) / 10;
k = num % 10;
if ((pow(i, 3) + pow(j, 3) + pow(k, 3)) == num)
return 1; // 是水仙花数返回非 0
return 0;
}