水仙花數 ps:各位數字立方和等於該數自己javascript
方法1 (數組)java
<script type="text/javascript"> var i = 100; var newi,num,num0,num1,num2; // var x; // x=window.getTime; t= new Date(); x = t.getTime(); // console.log(x); while (i <= 999) { i++; // console.log(i); newi=i.toString().split("");//分割3位數 num0=Number(newi[0]); num1=Number(newi[1]); num2=Number(newi[2]); num=(num0*num0*num0)+(num1*num1*num1)+(num2*num2*num2);//每一個數的立方相加 if (num==i)//判斷 { document.write(i+"<br />"); } } t= new Date(); y = t.getTime(); // console.log(y); document.write("耗時:"+(y-x)); </script>
方法2(求餘)數組
<script type="text/javascript"> var i = 100; var n1,n2,n3,num; t= new Date(); x = t.getTime(); while (i<=999) { // console.log(i); n1 = (i-i%100)/100;//百位數字 n2 = (i%100-i%10)/10;//十位數字 n3 = i%10;//個位數字 num=n1*n1*n1+n2*n2*n2+n3*n3*n3; if (i==num) { document.write(i+"<br>") }; ++i; }; t= new Date(); y = t.getTime(); // console.log(y); document.write("耗時:"+(y-x)); </script>
輸出九九乘法表spa
<script type="text/javascript"> document.write("<table rules=all align=center cellpadding=5 border=1 >"); for (var i = 1; i<=9; i++) { document.write("<tr>"); //行 for (var n = 1; n <= i; n++) { document.write("<td>"+n+"x"+i+"="+n*i+"</td>");//列 } document.write("</tr>");//行 } document.write("</table>"); </script>
取數組中的最大值和最小值以及各自的下標code
<script type="text/javascript"> var arr=[2,5,4,10,15,20,1] var n;//遍歷值 var big=arr[0];//取索引0 var small=arr[0];//取索引0 var x;//下標 var y;//下標 document.write(arr+"<br />"); for (var i = 0; i < arr.length; i++) { n=arr[i+1]//從索引1開始比 if (n>big) { big=n//置最大數 x=i+1//記錄最大下標 } if (n<small) { small=n//置最小數 y=i+1//記錄最小下標 } } document.write("最大:"+big+"下標:"+x+"<br />"); document.write("最小:"+small+"下標:"+y); </script>