标题: [其他] 批处理怎样乱序排列字符串里的字符? [打印本页]
作者: bluewing009 时间: 2010-12-4 09:38 标题: 批处理怎样乱序排列字符串里的字符?
本帖最后由 pcl_test 于 2016-9-28 22:19 编辑
RT,对abcdefghijklmnopqrstuvwxyz进行乱序排列
我的思路:
取随机数,从字符串取一个,然后去掉这个已经取得的,再次随机取一个,再去掉.....循环至最后一个
以下是代码- @echo off
- Setlocal enabledelayedexpansion
- set var_temp=abcdefghijklmnopqrstuvwxyz
- for /l %%i in (26,-1,1) do (
- set /a pick=!random!%%%%i
- call :1 !pick!
- )
- echo !var!
- pause
- :1
- set choose=!var_temp:~%1,1!
- set var=%var%%choose%
- call :2 !choose!
- goto :eof
- :2
- set var_temp=!var_temp:%1=!
- goto :eof
复制代码
问: 还有没有其他的快捷思路或方法?
[ 本帖最后由 bluewing009 于 2010-12-4 09:42 编辑 ]
作者: hanyeguxing 时间: 2010-12-4 09:52
- @echo off&Setlocal enabledelayedexpansion
- set t=abcdefghijklmnopqrstuvwxyz
- for /l %%a in (0,1,25) do set #!random!!random!!random!=!t:~%%a,1!
- for /f "tokens=2 delims==" %%a in ('set #') do set v=!v!%%a
- echo %v%&pause
复制代码
使用连续多个!random!是为了降低变量名重复几率的
[ 本帖最后由 hanyeguxing 于 2010-12-4 10:31 编辑 ]
作者: hanyeguxing 时间: 2010-12-4 10:16
如果担心%random%有重复,则:- @echo off&Setlocal enabledelayedexpansion
- set t=abcdefghijklmnopqrstuvwxyz
- :n
- set/a r=%random%%%26
- if "!#%r%!"=="" (set #%r%==) else goto:n
- set/a n+=1&set v=!v!!t:~%r%,1!
- if %n% lss 26 goto:n
- echo %v%&pause
复制代码
[ 本帖最后由 hanyeguxing 于 2010-12-4 10:17 编辑 ]
作者: vsbat 时间: 2010-12-4 11:41
- @echo off
- setlocal enabledelayedexpansion
-
- set var=abcdefghijklmnopqrstuvwxyz
- set x=26
- set c=0
-
- :Go
- set /a num=%random%%%%x%
- set t=!var:~%num%,1!
- set result=%result%%t%
- set var=!var:%t%=!
- set /a c+=1
- set /a x-=1
- if %c% == 26 (goto :End) else (goto :Go)
- :End
- echo %result%
- pause
复制代码
作者: hanyeguxing 时间: 2010-12-4 11:57
- @echo off&Setlocal enabledelayedexpansion
- set v=abcdefghijklmnopqrstuvwxyz
- for /l %%a in (26,-1,1) do (
- set/a n=!random!%%%%a
- for %%b in (!n!) do set t=!v:~%%b,1!
- for %%b in (!t!) do set v=!v:%%b=!&set e=!e!%%b
- )
- echo %e%&pause
复制代码
或:- @echo off&Setlocal enabledelayedexpansion
- set v=abcdefghijklmnopqrstuvwxyz
- for /l %%a in (26,-1,1) do (
- set/a n=!random!%%%%a
- call set t=%%v:~!n!,1%%
- call set v=%%v:!t!=%%
- set e=!e!!t!
- )
- echo %e%&pause
复制代码
[ 本帖最后由 hanyeguxing 于 2010-12-4 12:05 编辑 ]
作者: cutebe 时间: 2010-12-4 15:49
- @echo off
- Setlocal enabledelayedexpansion
- set ala=abcdefghijklmnopqrstuvwxyz
- :lp
- set/an=%random%%%26,m=n-1
- if %n%==0 goto :lp
- set ala=!ala:~%n%!!ala:~%m%,1!!ala:~,%m%!
- set/ax+=1
- if %x% leq 99 goto :lp
- echo %ala%
- pause
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |