Home >Web Front-end >JS Tutorial >Use javascript to realize whether the given sequence of boxes can be connected into a rectangle_javascript skills

Use javascript to realize whether the given sequence of boxes can be connected into a rectangle_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:09:201152browse
点击在新窗口中浏览此图片
by cpglkg
<script><br>var arr=[5,10,15];<br><br><br>function rect(arr,width)<br>{<br>   arr.sort(function(a,b){return a-b;});<br>   var len = arr.length;<br>   var temp = arr[0];<br>   var w = null;<br>   var h = null;<br>   for(var i = 0; i < len; i )<BR>   {<BR>       if((temp i) != arr[i])<BR>       {<BR>           w = i;<BR>           if (len % w != 0)<BR>               return false;<BR>           h = len/w;<BR>           break;<BR>       }<BR>   }<BR>   var r = width - w;<BR>   temp = r;<BR>   for(var i = w; i < len; i )<BR>   {<BR>       if(arr[i] != temp i)<BR>       {<BR>           temp = r;<BR>           if(arr[i] != temp i)<BR>           {<BR>               return false;<BR>           }<BR>       }<BR>   }<br><br>   return true;<BR>}<BR>alert(rect(arr,6));<BR></script>


by 无殇
<script><br>var rows=6;<br>function ch(array){<br>array=array.sort(function(a,b){return a-b;})<br>var n,c=0;<br>for(var i=1;i<array.length;i )<BR>if(array[i]-array[i-1]==1)<BR>c ;<BR>else<BR>break;<BR>if(array.length%(c 1)!=0)return false;<BR>for(var i=1;i<array.length;i )<BR>{<BR>n=array[i]-array[i-1];<BR>if(n!=1 && n c!=rows)<BR>return false;<BR>}<BR>return true;<BR>}<BR>alert(ch([1,2,3]))<BR>alert(ch([5,6,11,12,17,18,23,24]))<BR>alert(ch([19,25,31]))<BR>alert(ch([1,2,3,4,7,8]))<BR></script>


by 秦皇也爱JS  

<script><br>  var arr = [5,10,15];<br>  var width = 6;  <br><br>  arr = arr.sort(function(a,b){ return a-b});<br>  var row = Math.ceil((arr[arr.length-1]-arr[0] 1)/width);<br>  var col = Math.floor(arr.length / row);<br><br>  check();<br>  function check(){<br>    for(var i=0;i<arr.length-1;i ){<BR>      if(arr[i 1] - arr[i] !=1 && i%col!=col-1 || i%col==col-1 && arr[i 1] - arr[i] != width-col 1){<BR>        alert(false);<BR>        return;<BR>      }<BR>    }<BR>    alert(true);<BR>  }<BR> </script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn