数学趣题——寻找水仙花数
一个3位数若等于各位的立方和,即是水仙花数
源码如下:
1: #include <stdio.h>
2: 3: int IsNar(int a);
4: void Nar();
5: 6: int main()
7: { 8: Nar();9: return 0;
10: } 11: 12: void Nar()
13: {14: int i;
15: for (i=100; i<999; i++)
16: {17: if (IsNar(i))
18: {19: printf("%d ", i);
20: } 21: } 22: } 23: 24: int IsNar(int a)
25: {26: int sum = 0, tmp;
27: tmp = a;28: while (tmp > 0)
29: { 30: sum += (tmp % 10) * (tmp %10) * (tmp %10); 31: tmp /= 10; 32: } 33: 34: if (sum == a)
35: return 1;
36: else
37: return 0;
38: }

浙公网安备 33010602011771号