1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="UTF-8">
5 <title></title>
6 <script type="text/javascript">
7
8 /*
9 * 水仙花数是指一个3位数,它的每个位上的数字的3 次幂之和等于它本身。
10 (例如:1^3 + 5^3 + 3^3 = 153),请打印所有的水仙花数。
11 */
12
13 //------------------------------------------------------------------
14
15 //打印所有的三位数
16 for(var i=100 ; i<1000 ; i++){
17
18 //获取i的百位 十位 个位的数字
19 //获取百位数字
20 var bai = parseInt(i/100);
21
22 //获取十位的数字
23 var shi = parseInt((i-bai*100)/10);
24
25 //获取个位数字
26 var ge = i % 10;
27
28 //判断i是否是水仙花数
29 if(bai*bai*bai + shi*shi*shi + ge*ge*ge == i){
30 console.log(i);
31 }
32
33
34 }
35
36
37 </script>
38 </head>
39 <body>
40 </body>
41 </html>