本帖最后由 caruko 于 2011-5-21 14:58 编辑
不用CALL的办法,但目前不支持重复字符,因为用到字符替换,数字比如“2 12” “a ab bf”就会出错。
去重复也可以,但是需要增加一些代码,把字符改成"_2_ _12_",效率有降低。
纯数字的话,稍改一下效率更快。- @echo off
- SETLOCAL ENABLEDELAYEDEXPANSION
- set "str=a b c d e f"
- for %%i in (%str%) do (
- set /a n+=1,_%%i=n
- )
- echo, !str!
- for /l %%a in (1,1,10000000) do (
- set "last="&set "flag="&set "pos=0"
- for %%b in (!str!) do (
- set /a pos+=1
- if defined last (
- set /a n1=_%%b,n2=_!last!
- if !n1! gtr !n2! set flag=!last! !pos!
- set "last=%%b"
- ) else (
- set "last=%%b"
- )
- )
- if not defined flag call :end %%a
- for /f %%b in ("!flag!") do for %%c in (!str!) do if !_%%c! gtr !_%%b! set "th=%%c"
- for /f "tokens=1,3" %%b in ("!flag! !th!") do (
- set "temp=!str:%%b=#!"
- set "temp=!temp:%%c=%%b!"
- set "str=!temp:#=%%c!"
- )
- set "ppos="&set "cut1="&set "cut2="&set "array="
- for %%b in (!str!) do (
- set /a ppos+=1
- for /f "tokens=2" %%c in ("!flag!") do (
- if !ppos! geq %%c (
- set "cut2=!cut2! %%b"
- ) else (
- set "cut1=!cut1! %%b"
- )
- )
- )
- for %%i in (!cut2!) do (
- set "array=%%i !array!"
- for %%j in (!array!) do (
- if %%i gtr %%j (
- set "array=!array:%%i=#!"
- set "array=!array:%%j=%%i!"
- set "array=!array:#=%%j!"
- )
- )
- )
- set str=!cut1! !array!
- echo,!str!
- )
- :end
- echo,一共%1个排列.
- pause>nul&exit
复制代码
|