0
点赞
收藏
分享

微信扫一扫

js中求水仙花数

纽二 2022-05-07 阅读 122
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>Document</title>
6 <script type="text/javascript">
7 //水仙花数
8 for (var num = 100; num <= 999; num++) {
9
10 var str = num.toString();
11 var res1 =Number(str.slice(0,1)) //百位 或者用charAt(0)
12 var res2 =Number(str.slice(1,2)) //十位 或者用charAt(1)
13 var res3 =Number(str.slice(2,3)) //个位 或者用charAt(2)
14
15 var sumcube = Math.pow(res1,3)+Math.pow(res2,3)+Math.pow(res3,3); //立方和
16 num = res1*100+res2*10+res3; //这个数
17 if(sumcube == num){
18 console.log(num);
19 }
20 }
21 </script>
22 </head>
23 <body>
24
25 </body>
26 </html>



你要保守你心,胜过保守一切。

作者:刘俊涛的博客​



举报

相关推荐

0 条评论