qfedu-c-level/day4/homework/h13.c

38 lines
848 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.

// 【扩展题1】请编程输出0-1000以内的水仙花数
// 【提示】水仙花数算法:一个数 = 它各位的立方和,例如 : 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3
#include <stdio.h>
void fun1()
{
int i, j, k;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
for (k = 0; k < 10; k++)
{
if (i * 100 + j * 10 + k == i * i * i + j * j * j + k * k * k)
printf("%d\n", i * 100 + j * 10 + k);
}
}
}
}
void fun2()
{
int x = 0;
while (x++ < 1000 - 1) // -1 是因为 1000 不是水仙花数
{
int a = x / 100;
int b = x / 10 % 10;
int c = x % 10;
if (x == a * a * a + b * b * b + c * c * c)
printf("%d\n", x);
}
}
int main()
{
fun2();
return 0;
}