c - 水仙花数.

 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 /*
 5 *打印出所有的“水仙花数” ,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。
 6 *例如:153 是一个“水仙花数” ,因为 153=1 的三次方+5 的三次方+3 的三次方。
 7 *程序分析:利用 循环控制 100-999 的数,每个数分解出个位,十位,百位.
 8 */
 9 int
10 main(void) {
11     int ones;    //个位.
12     int tens;    //十位.
13     int hundreds;    //百位.
14     int tmp;
15     for(int i = 100; i <= 999; i++) {
16         ones = i%10;
17         tens = i/10%10;
18         hundreds = i/100;
19         tmp = (int)pow((double)ones, 3) + (int)pow((double)tens, 3) + (int)pow((double)hundreds, 3);
20         if(tmp == i)
21         printf("%d\n", i);
22     }
23 }

 

posted @ 2014-11-25 09:49  wonkju  阅读(229)  评论(0)    收藏  举报