使用对角分割解螺旋方阵:- @echo off
- :s
- setlocal enabledelayedexpansion&cls
- set/p n=输入阶数:
- if %n% gtr 46340 goto:s
- set/a k=n*n&set k= !k!
- for /l %%a in (1,1,11) do if not "!k:~%%a,1!"=="" set q=%%a&set "s=!s! "
- for /l %%a in (1,1,%n%) do (
- for /l %%b in (1,1,%n%) do (
- set/a k=n+1-%%a
- if %%b geq %%a if %%b leq !k! set/a "p=4*(n-%%a)*%%a-4*n+7*%%a+%%b-3"
- if %%b leq %%a if %%b gtr !k! set/a "p=4*(n-%%a)*%%a-2*n+5*%%a-%%b-1"
- if %%b gtr %%a if %%b gtr !k! set/a "p=4*(n-%%b)*%%b-2*n+%%a+3*%%b-1"
- if %%b lss %%a if %%b leq !k! set/a "p=4*(n-%%b)*%%b-%%a+%%b+1"
- set p=%s%!p!&set/p"=!p:~-%q%! "<nul
- )
- echo;
- )
- pause&endlocal&goto:s
复制代码 JS版:- <script>
- hy(20);
- function hy( n)
- { var i,j;
- document.write("<body bgcolor=#008000><center><table border=1 bgcolor=#B0E2FF bordercolor=#fff000> ");
- for(i=1;i<=n;i++){document.write("<tr>");
- for(j=1;j<=n;j++){
- var Han=4*(n-i)*i-4*n+7*i+j-3;
- var Ye=4*(n-j)*j-2*n+i+3*j-1;
- var Gu=4*(n-i)*i-2*n+5*i-j-1;
- var Xing=4*(n-j)*j+-i+j+1;
- document.write("<td>"+((i<=j&&i+j<=n+1)?Han:(i<j&&i+j>n+1)?Ye:(i>=j&&i+j>n+1)?Gu:Xing)+"</td>");} document.write("</tr>");}
- document.write("</table> ");
- }
- </script>
复制代码 [ 本帖最后由 hanyeguxing 于 2011-2-8 11:15 编辑 ] |