[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
=.=
发表对别人的看法的同时,也看看自己。
你可以坚持自己的想法,但不能强迫别人也有兴趣。
不依自己的想法就是"XX主义"?

TOP

本帖最后由 caruko 于 2011-5-21 14:58 编辑

不用CALL的办法,但目前不支持重复字符,因为用到字符替换,数字比如“2 12” “a  ab  bf”就会出错。
去重复也可以,但是需要增加一些代码,把字符改成"_2_  _12_",效率有降低。
纯数字的话,稍改一下效率更快。
  1. @echo off
  2. SETLOCAL ENABLEDELAYEDEXPANSION
  3. set "str=a b c d e f"
  4. for %%i in (%str%) do (
  5.     set /a n+=1,_%%i=n
  6. )
  7. echo, !str!
  8. for /l %%a in (1,1,10000000) do (
  9.     set "last="&set "flag="&set "pos=0"
  10.     for %%b in (!str!) do (
  11.         set /a pos+=1
  12.         if defined last (
  13.             set /a n1=_%%b,n2=_!last!
  14.             if !n1! gtr !n2! set flag=!last! !pos!
  15.             set "last=%%b"
  16.         ) else (
  17.             set "last=%%b"
  18.         )
  19.     )
  20.     if not defined flag call :end %%a
  21.     for /f %%b in ("!flag!") do for %%c in (!str!) do if !_%%c! gtr !_%%b! set "th=%%c"
  22.     for /f "tokens=1,3" %%b in ("!flag! !th!") do (
  23.         set "temp=!str:%%b=#!"
  24.         set "temp=!temp:%%c=%%b!"
  25.         set "str=!temp:#=%%c!"
  26.     )
  27.     set "ppos="&set "cut1="&set "cut2="&set "array="
  28.     for %%b in (!str!) do (
  29.         set /a ppos+=1
  30.         for /f "tokens=2" %%c in ("!flag!") do (
  31.             if !ppos! geq %%c (
  32.                 set "cut2=!cut2! %%b"
  33.             ) else (
  34.                 set "cut1=!cut1! %%b"
  35.             )
  36.         )
  37.     )
  38.     for %%i in (!cut2!) do (
  39.         set "array=%%i !array!"
  40.         for %%j in (!array!) do (
  41.             if %%i gtr %%j (
  42.                 set "array=!array:%%i=#!"
  43.                 set "array=!array:%%j=%%i!"
  44.                 set "array=!array:#=%%j!"
  45.             )
  46.         )
  47.     )
  48.     set str=!cut1! !array!
  49.     echo,!str!
  50. )
  51. :end
  52. echo,一共%1个排列.
  53. pause>nul&exit
复制代码
1

评分人数

TOP

不用递归的算法,对字符位置敏感,去重复有点麻烦。

TOP

本帖最后由 caruko 于 2011-5-21 17:07 编辑

不用CALL,不用递归的办法,应该是批处理最快的方法之一吧,比CALL 递归速度要快3倍以上吧。
但是跳出FOR还是没有好的办法,目前用CALL EXIT退出循环。
不支持重复字符,因为对字符的位置敏感,重复字符的原位置值判断麻烦。

稍作修改,在原字符前后各加一个@,以保证不会意外的替换掉字符的一部分,只要字符不带@以及特殊字符即可。

代码可以得到任意字符的一个排列的下一个排列,或者得到第N个排列,比如排列字符为abcdef,现求 abdfec 的下一个排列,只要在初始化位置值后,替换STR为abdfec,计算一次以后即得到 abecdf。

支持多字符元素的组合,如代码:
  1. @echo off&SETLOCAL ENABLEDELAYEDEXPANSION
  2. set "str=1 11 12 a ab acd"
  3. set time1=!time!
  4. for %%i in (%str%) do (
  5.     set /a n+=1,_@%%i@=n
  6.     set "tstr=!tstr! @%%i@"
  7. )
  8. set "str=!tstr!"
  9. echo,!str:@=!
  10. for /l %%a in (1,1,10000000) do (
  11.     set "last="&set "flag="&set "pos=0"
  12.     for %%b in (!str!) do (
  13.         set /a pos+=1
  14.         if defined last (
  15.             set /a n1=_%%b,n2=_!last!
  16.             if !n1! gtr !n2! set flag=!last! !pos!
  17.             set "last=%%b"
  18.         ) else (
  19.             set "last=%%b"
  20.         )
  21.     )
  22.     if not defined flag call :end %%a
  23.     for /f %%b in ("!flag!") do for %%c in (!str!) do if !_%%c! gtr !_%%b! set "th=%%c"
  24.     for /f "tokens=1,3" %%b in ("!flag! !th!") do (
  25.         set "temp=!str:%%b=#!"
  26.         set "temp=!temp:%%c=%%b!"
  27.         set "str=!temp:#=%%c!"
  28.     )
  29.     set "ppos="&set "cut1="&set "cut2="&set "array="
  30.     for %%b in (!str!) do (
  31.         set /a ppos+=1
  32.         for /f "tokens=2" %%c in ("!flag!") do (
  33.             if !ppos! geq %%c (
  34.                 set "cut2=!cut2! %%b"
  35.             ) else (
  36.                 set "cut1=!cut1! %%b"
  37.             )
  38.         )
  39.     )
  40.     for %%i in (!cut2!) do (
  41.         set "array=%%i !array!"
  42.         for %%j in (!array!) do (
  43.             if %%i gtr %%j (
  44.                 set "array=!array:%%i=#!"
  45.                 set "array=!array:%%j=%%i!"
  46.                 set "array=!array:#=%%j!"
  47.             )
  48.         )
  49.     )
  50.     set str=!cut1! !array!
  51.     echo,!str:@=!
  52. )
  53. :end
  54. echo,一共%1个排列. !time1!--^>!time!
  55. pause>nul&exit
复制代码

TOP

23# 523066680
  1. @echo off
  2. set a=%%a&set b=%%b&set c=%%c
  3. set "fo=for %a% in (a b c) do for %b% in (a b c) do for %c% in (a b c) do if not %a%==%b% if not %b%==%c% if not %a%==%c% echo %a%%b%%c%"
  4. %fo%
复制代码

TOP

=.=
好高明的算法,看了半天还没弄明白

TOP

返回列表