中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-04 20:45
48,038 topics / 350,123 posts / today 0 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » [Closed] Permutations and Combinations
Printable Version  6,467 / 36
Floor1 pusofalse Posted 2008-06-29 05:35
银牌会员 Posts 646 Credits 1,604
Just give a group of strings, such as a b c
List all permutations of a b c, that is, the output is as follows

a b c
a c b
b a c
b c a
c a b
c b a

I use the brute-force method, triple for loops can get such results, but the number of characters is not necessarily...
Like this... Ask the expert... Hope to summarize an efficient and practical method...

[ Last edited by pusofalse on 2008-12-28 at 03:32 ]
Floor2 pusofalse Posted 2008-06-29 10:22
银牌会员 Posts 646 Credits 1,604
Worked on it for a whole morning, and then found out there's a major BUG. It turns out that only when there are three characters can all combinations be arranged. If there are more or fewer, it goes wrong. It has to be 3. I'm asking all the experts, is there a good way?



[ Last edited by pusofalse on 2008-6-29 at 10:29 AM ]
Floor3 bat-zw Posted 2008-06-29 11:04
金牌会员 Posts 1,276 Credits 3,105
I don't know if this meets the requirements:
Floor4 pusofalse Posted 2008-06-29 11:07
银牌会员 Posts 646 Credits 1,604
The code given by Senior ZW also has bugs... It can only arrange combinations of four characters. If there are more than a few, only the first four are retained... If there are fewer, an error of dividing by zero will occur... The randomly given string, the number of strings is unknown...
Floor5 bat-zw Posted 2008-06-29 11:29
金牌会员 Posts 1,276 Credits 3,105
Originally posted by pusofalse at 2008-6-29 11:07:
There are also bugs in the code provided by前辈 ZW... It can only arrange combinations of four characters. If there are more than a few, it only keeps the first four... If there are fewer, there will be an error of dividing by zero...
The number of randomly given strings is unknown...

The indeterminate number of characters is indeed a big problem. The following code basically achieves it (I think it's quite complicated):


[ Last edited by zw19750516 on 2008-6-29 at 12:04 PM ]
Floor6 pusofalse Posted 2008-06-29 11:35
银牌会员 Posts 646 Credits 1,604
Thanks a lot! Learning~
Floor7 bat-zw Posted 2008-06-29 11:37
金牌会员 Posts 1,276 Credits 3,105
Originally posted by pusofalse at 2008-6-29 11:35:
Thanks a lot! Learning~

Such a method will be terribly slow when the number of characters exceeds six! Still need to find other methods.
Floor8 pusofalse Posted 2008-06-29 11:46
银牌会员 Posts 646 Credits 1,604
Hmm, it's tried. It's okay for less than 6 digits... Also, there's another bug. When entering a string like "b e e n" with two identical characters in it, it will go wrong.
Floor9 HAT Posted 2008-06-29 11:52
版主 Posts 5,017 Credits 9,023
When I studied "Algorithm Design" before, I wrote such a C code, which is a recursive algorithm.

Floor10 bat-zw Posted 2008-06-29 11:52
金牌会员 Posts 1,276 Credits 3,105
Characters that are the same are easy to handle:
Add in the batch processing "for %%i in (%codes%) do set a=#%%i#&set b=!b! !a!", and then make corresponding processing. The specific code I won't write. It's just that the efficiency of the high position seems to be unable to be solved with batch processing, because using the method of set var=%var% %str% in my second floor will be limited by the total number of characters.

[ Last edited by zw19750516 on 2008-6-29 at 12:02 PM ]
Floor11 plp626 Posted 2008-06-29 13:37
银牌会员 Posts 1,020 Credits 2,278
```@echo off&setlocal enabledelayedexpansion
set s1=a b c
for %%a in (!s1!) do (set s2=!s1:%%a=!
for %%b in (!s2!) do (set s3=!s2:%%b=!
for %%c in (!s3!) do (echo %%a%%b%%c
) ) )
pause

The 6-digit code is similar to```
Floor12 bat-zw Posted 2008-06-29 13:47
金牌会员 Posts 1,276 Credits 3,105
Finally improved a little bit of efficiency, local test permuting six-digit numbers takes nearly 10 minutes:
Floor13 plp626 Posted 2008-06-29 14:07
银牌会员 Posts 1,020 Credits 2,278
7-bit takes 2.28 seconds
timediff.bat can be found in the forum p function library (used to calculate the difference between two time points)
Floor14 bat-zw Posted 2008-06-29 16:33
金牌会员 Posts 1,276 Credits 3,105
Originally posted by plp626 at 2008-6-29 14:07:
7 characters took 2.28 seconds
timediff.bat can be found in the forum's p function library (used to calculate the difference between two time points)
Floor15 slore Posted 2008-06-29 17:19
铂金会员 Posts 2,478 Credits 5,212
Generate an n-layer nested bat and call it =.=

Bat seems to be able to achieve "recursion" using parameters... But I haven't practiced this... Brain cells...
1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023