效率还可以,但不支持重复字符。
7个字符,5040组合,用时不到15秒。 | @echo off&SETLOCAL ENABLEDELAYEDEXPANSION | | set "str=a b c d e f g" | | set time1=!time! | | 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个排列. !time1!--^>!time! | | pause>nul&exitCOPY |
|