C语言输出所有水仙花数

题目要求:

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
int main() {
// 输出所有水仙花数
/*
思路:水仙花数是三位数 所以循环从100开始到999;
提取个十百位数 分别乘以立方和 再去判断是否满足水仙花要求,
然后在循环体内输出
*/
int ge = 0, shi = 0, bai = 0;
for (int i = 100; i < 1000; i++) {
int ge = i % 10;
int shi = i % 100 / 10;
int bai = i / 100;
if((ge*ge*ge)+shi*shi*shi+bai*bai*bai==i) {
printf("%d\n", i);
}
}

}

C语言输出所有水仙花数
http://example.com/2023/08/18/C语言输出所有水仙花数/
Author
fwlw
Posted on
August 18, 2023
Licensed under