标题: [数值计算] [已解决]批处理循环输出排列组合提示语法错误 [打印本页]
作者: firewolf85 时间: 2009-11-13 09:14 标题: [已解决]批处理循环输出排列组合提示语法错误
请高手指点下我的代码哪里有问题,应该怎么改正。- @echo off
- for /l %%i in (1 1 8) do (
- for /l %%j in (1 1 8) do (
- for /l %%k in (1 1 8) do (
- for /l %%l in (1 1 8) do (
- for /l %%m in (1 1 8) do (
- for /l %%n in (1 1 8) do (
- for /l %%o in (1 1 8) do (
- for /l %%p in (1 1 8) do (
- set /a a=%%i+%%j+%%k+%%l+%%m+%%n+%%o+%%p
- if not %a == 36 goto :next
- set /a b=%%i*%%j*%%k*%%l*%%m*%%n*%%o*%%p
- if not %b == 40320 goto :next
- echo %%i %%j %%k %%l %%m %%n %%o %%p
- :next
- )
- )
- )
- )
- )
- )
- )
- )
- pause
复制代码
这段代码是为了输出 1-8这8个数的全排列
[ 本帖最后由 firewolf85 于 2009-11-13 14:23 编辑 ]
作者: vsbat 时间: 2009-11-13 12:31
@echo off
setlocal enabledelayedexpansion ::for 内部要变量延迟
for /l %%i in (1 1 8) do (
for /l %%j in (1 1 8) do (
for /l %%k in (1 1 8) do (
for /l %%l in (1 1 8) do (
for /l %%m in (1 1 8) do (
for /l %%n in (1 1 8) do (
for /l %%o in (1 1 8) do (
for /l %%p in (1 1 8) do (
set /a a=%%i+%%j+%%k+%%l+%%m+%%n+%%o+%%p
if not !a! == 36 goto :next ::for 内部要变量延迟
set /a b=%%i*%%j*%%k*%%l*%%m*%%n*%%o*%%p
if not !b! == 40320 goto :next ::for 内部要变量延迟
echo %%i %%j %%k %%l %%m %%n %%o %%p
:next
echo hello ::不可无内容
)
)
)
)
)
)
)
)
pause
[ 本帖最后由 vsbat 于 2009-11-13 12:33 编辑 ]
作者: batman 时间: 2009-11-13 13:20
以前写的枚举输入字符组合的方法(注意非空字符不要超过9个):- @echo off&setlocal enabledelayedexpansion
- set /p num=请输入字符:
- set "num=%num: =%"&set "num=!num: =!"
- set "s=abcdefghijklnmopqrstuvwxyz"
- :lp
- set /a n+=1
- set "ss=%ss%str%n%"&set "k=%k%)"&set "str1=%str1% %n%"
- set "nums=%nums% %num:~,1%"&set "num=%num:~1%
- if defined num goto lp
- (echo ^@echo off^&setlocal enabledelayedexpansion
- echo cd.^>list.txt
- echo for %%%%a in ^(%nums%^) do set /a m+=1^&set "_^!m^!=%%%%a"
- echo set "str1=%str1%")>temp.bat
- for /l %%a in (1,1,%n%) do (
- set "a=%%%%!s:~,1!"&set "b=!ss:~,4!"&set "c=!ss:~4,4!"
- set "str=!str!^!_!a!^!"&if "!c!" equ "" set "c=flag"
- echo for !a! in ^(^^^!!b!^^^!^) do ^(set "!c!=^!!b!:!a!=^!">>temp.bat
- set "s=!s:~1!"&set "ss=!ss:~4!"
- )
- echo echo !str!^>^>list.txt>>temp.bat
- echo %k%>>temp.bat
- echo start list.txt^&del /q %%0>>temp.bat
- endlocal&temp.bat
复制代码
[ 本帖最后由 batman 于 2009-11-13 18:03 编辑 ]
作者: firewolf85 时间: 2009-11-13 13:53
感谢两位的热心帮助,二楼提出“echo hello ::不可无内容”确实解决程序提示语法错误的问题,但是开启变量延迟后并没有解决最后正确结果的问题,希望各位大侠能有进一步的指点。或者说我这种思路在批处理中不能实现。三楼版主的代码试了,结果确实是我想要的,正在研究当中ING....
[ 本帖最后由 firewolf85 于 2009-11-13 13:56 编辑 ]
作者: batman 时间: 2009-11-13 13:59
for中是不能有goto循环的,只能跳转到外部循环
作者: firewolf85 时间: 2009-11-13 14:13
看来问题确实出在代码上,修改后的代码可以正确运行了,不过效率那个不怎么样。- @echo off&setlocal enabledelayedexpansion
- for /l %%i in (1 1 8) do (
- for /l %%j in (1 1 8) do (
- for /l %%k in (1 1 8) do (
- for /l %%l in (1 1 8) do (
- for /l %%m in (1 1 8) do (
- for /l %%n in (1 1 8) do (
- for /l %%o in (1 1 8) do (
- for /l %%p in (1 1 8) do (
- set /a a=%%i+%%j+%%k+%%l+%%m+%%n+%%o+%%p
- set /a b=%%i*%%j*%%k*%%l*%%m*%%n*%%o*%%p
- if !a! ==36 if !b! == 40320 echo %%i %%j %%k %%l %%m %%n %%o %%p
- )
- )
- )
- )
- )
- )
- )
- )
- pause
复制代码
作者: firewolf85 时间: 2009-11-13 14:15
刚在调试修改,没看到版主的回覆,原来是这样子啊,谢谢斑竹指点。
作者: batman 时间: 2009-11-13 23:18
变量嵌套+递归法解本题:- @echo off&setlocal enabledelayedexpansion
- set "str1=1 2 3 4 5 6 7 8"
- call :lp 1
- start list.txt&goto :eof
- :lp
- for %%a in (!str%1!) do (
- set "var%1=%%a"
- if not defined _%1 set "var=!var!^!var%1^!"&set "_%1=a"
- set /a n=%1+1&set "str!n!=!str%1:%%a=!"
- if "!n!" leq "8" (
- call :lp !n!
- ) else (
- for %%a in (!var!) do echo %%a>>list.txt
- )
- )
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |