本帖最后由 风之语故乡 于 2011-8-19 00:36 编辑
这是我自己写的,很粗陋了。- @echo off
- setlocal enabledelayedexpansion
- set mdebug=0
- if %mdebug%==1 (set "trace=echo") else (set "trace=rem")
- %trace%.调试
- echo.鉴于排版和显示问题,输入的数字不要大于9
- echo.
- set /p input=你想要的螺旋方阵是几阶的?请输入任意数字:
- set /p input2=顺时针还是逆时针?顺时针输入1,逆时针输入2:
-
- for /l %%i in (1,1,%input%) do (
- for /l %%j in (1,1,%input%) do (
- set "数组[%%i][%%j]=0"
- )
- )
-
- set "方向=0"
- set /a "数组长=input*input"
- set "行=1"
- set "列=1"
- set "数组[%行%][%列%]=01"
-
- for /l %%j in (2,1,%数组长% ) do (
-
- if !方向!==0 (
- if !列!==%input% (set "bool1=1") else (set "bool1=0")
- set /a "t=!列!+1"
- for /f "delims=" %%a in ("数组[!行!][!t!]") do (set "t2=!%%a!")
- if !t! leq %input% (if !t2! neq 0 (set "bool2=1") else (set "bool2=0"))
- set /a "bool3=bool1|bool2"
- if !bool3!==1 (
- set /a "方向=(!方向!+1)%%4"
- ))
-
- if !方向!==1 (
- if !行!==%input% (set "bool1=1") else (set "bool1=0")
- set /a "t=!行!+1"
- for /f "delims=" %%a in ("数组[!t!][!列!]") do (set "t2=!%%a!")
- if !t! leq %input% (if !t2! neq 0 (set "bool2=1") else (set "bool2=0"))
- set /a "bool3=bool1|bool2"
- if !bool3!==1 (
- set /a "方向=(!方向!+1)%%4"
- ))
-
- if !方向!==2 (
- if !列!==1 (set "bool1=1") else (set "bool1=0")
- set /a "t=!列!-1"
- for /f "delims=" %%a in ("数组[!行!][!t!]") do (set "t2=!%%a!")
- if !t! geq 1 (if !t2! neq 0 (set "bool2=1") else (set "bool2=0"))
- set /a "bool3=bool1|bool2"
- if !bool3!==1 (
- set /a "方向=(!方向!+1)%%4"
- ))
-
- if !方向!==3 (
- if !行!==1 (set "bool1=1") else (set "bool1=0")
- set /a "t=!行!-1"
- for /f "delims=" %%a in ("数组[!t!][!列!]") do (set "t2=!%%a!")
- if !t! geq 1 (if !t2! neq 0 (set "bool2=1") else (set "bool2=0"))
- set /a "bool3=bool1|bool2"
- if !bool3!==1 (
- set /a "方向=(!方向!+1)%%4"
- ))
-
- if !方向!==0 ( set /a "列=!列!+1" )
- if !方向!==1 ( set /a "行=!行!+1" )
- if !方向!==2 ( set /a "列=!列!-1" )
- if !方向!==3 ( set /a "行=!行!-1" )
- set /a "t3=%%j+100"
- set "数组[!行!][!列!]=!t3:~1!"
- )
-
- if %input2%==2 (
- for /l %%i in (1,1,%input%) do (
- for /l %%j in (1,1,%input%) do (
- (set /p "=!数组[%%j][%%i]! ") < nul
- )
- echo.
- )
- ) else (
- for /l %%i in (1,1,%input%) do (
- for /l %%j in (1,1,%input%) do (
- (set /p "=!数组[%%i][%%j]! ") < nul
- )
- echo.
- )
- )
-
- pause >nul
复制代码 关于为啥数组要 加[][]是因为当n>11时,变量会重复比如:
数组111你说,到底是11行1列还是1行11列?
下面是我的第二种方法:- @echo off
- setlocal enabledelayedexpansion
- set mdebug=0
- if %mdebug%==1 (set "trace=echo") else (set "trace=rem")
- %trace%.调试
- echo.鉴于排版和显示问题,输入的数字不要大于9
- set /p n=你想要的螺旋方阵是几阶的?请输入任意数字:
- set /p input=顺时针还是逆时针?顺时针输入1,逆时针输入2:
- echo.
- set /a "nn=n*n"
- set "i=1"&set "j=1"
- set /a "圈=(n+1)/2"
-
- set "阶=!n!"
- set "首位数值=1"
- set "首位=1"
- for /l %%a in (1,1,%圈%) do (
- call :构造边缘 !首位数值! !阶! !首位!
- set /a "首位数值+=(阶-1)*4"
- set /a "阶-=2"
- set /a "首位+=1"
- )
-
-
-
- if %input%==2 (
- for /l %%i in (1,1,%n%) do (
- for /l %%j in (1,1,%n%) do (
- (set /p "=!数组[%%j][%%i]! ") < nul
- )
- echo.
- )
- ) else (
- for /l %%i in (1,1,%n%) do (
- for /l %%j in (1,1,%n%) do (
- (set /p "=!数组[%%i][%%j]! ") < nul
- )
- echo.
- )
- )
- %trace%.调试
- pause >nul
- goto :eof
-
- :构造边缘
- if %2==1 (
- set /a "t=%1+100"
- set "数组[%3][%3]=!t:~1!"
- ) else (
- set /a "圈长=(%2-1)*4"
- set "i=%3"&set "j=%3"
- set /a 末尾数值=%1+!圈长!-1
- set "头=%3"
- set /a "尾=头+%2-1"
- for /l %%b in (%1,1,!末尾数值!) do (
- set /a "t=%%b+100"
- set "数组[!i!][!j!]=!t:~1!"
- set "key1=1"
- if !key1!==1 (if !i!==!头! (if !j! lss !尾! (
- set /a j+=1
- set "key1=0"
- )))
- if !key1!==1 (if !j!==!尾! (if !i! lss !尾! (
- set /a i+=1
- set "key1=0"
- )))
- if !key1!==1 (if !i!==!尾! (if !j! gtr !头! (
- set /a j-=1
- set "key1=0"
- )))
- if !key1!==1 (if !j!==!头! (if !i! gtr !头! (
- set /a i-=1
- set "key1=0"
- )))
- )
- )
复制代码
|