China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-20 01:38
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] Permutations and Combinations View 6,107 Replies 36
Original Poster Posted 2008-06-29 05:35 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
zzz19760225 +2 2017-11-30 13:27
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 2 Posted 2008-06-29 10:22 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
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?

@echo off&setlocal enabledelayedexpansion
del ts.txt>nul 2>nul
set var=1 2 3
:1
if defined var5 set var=%var5%
for %%a in (%var%) do (
set dx=%%a
set/a mn+=1
set !mn!=%%a
call :lp
)

if defined had echo 完成&pause>nul&exit/b
for /l %%a in (%mn% -1 1) do set "var5=!var5!!%%a! "
set had=had
goto 1


:lp
set var1=!var:%dx%=!
for %%a in (%var1%) do (
set "var3=!var3!%%a "
)
>>ts.txt echo %var3%%dx%
set "%var3%%dx%=abcd"
set var3=


[ Last edited by pusofalse on 2008-6-29 at 10:29 AM ]
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 3 Posted 2008-06-29 11:04 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
I don't know if this meets the requirements:

@echo off&setlocal enabledelayedexpansion
if exist temp.txt del /q temp.txt
:begin
set code=abcd&set str=&set n=4
:again
set /a a=%random%%%%n%
set a=!code:~%a%,1!
set code=!code:%a%=!
set str=%str%%a%
set /a n-=1
if "%str:~3%" equ "" goto again
if defined var for %%i in (%var%) do if "%%i" equ "%str%" goto begin
echo %str%>>temp.txt
set var=%var% %str%
set /a m+=1
if %m% lss 24 goto begin
sort temp.txt&del /q temp.txt
pause>nul
批处理之家新域名:www.bathome.net
Floor 4 Posted 2008-06-29 11:07 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
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...
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 5 Posted 2008-06-29 11:29 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
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):

@echo off&setlocal enabledelayedexpansion
if exist temp.txt del /q temp.txt
:enter
cls&set /p codes=Please enter the character group (separate characters with spaces):
if not defined codes goto enter
for %%i in (%codes%) do set /a m+=1
set /a num=1,x=m,y=m-1
:lp
set /a num*=m
set /a m-=1
if %m% neq 0 goto lp
:begin
set code=!codes: =!&set str=&set /a n=x
:again
set /a a=%random%%%%n%
set a=!code:~%a%,1!
set code=!code:%a%=!
set str=%str%%a%
set /a n-=1
if "!str:~%y%!" equ "" goto again
if exist temp.txt for /f "delims=" %%i in (temp.txt) do if "%%i" equ "%str%" goto begin
echo %str%>>temp.txt
set /a m+=1
if %m% lss %num% goto begin
cls&sort temp.txt&del /q temp.txt
pause>nul


[ Last edited by zw19750516 on 2008-6-29 at 12:04 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
pusofalse +8 2008-06-29 11:34
批处理之家新域名:www.bathome.net
Floor 6 Posted 2008-06-29 11:35 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
Thanks a lot! Learning~
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 7 Posted 2008-06-29 11:37 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
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.
批处理之家新域名:www.bathome.net
Floor 8 Posted 2008-06-29 11:46 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
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.
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 9 Posted 2008-06-29 11:52 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
When I studied "Algorithm Design" before, I wrote such a C code, which is a recursive algorithm.

/*This program runs successfully under TC2.0*/
/*Algorithm 5.7 PERMUTATIONS1*/
#include<stdio.h>
int P;
void print(int P,int n)
{
int i;
printf("\n");
for(i=1;i<=n;i++)
printf("%d",P);
sleep(1);
}

void perm1(int m,int n)
{
int j,t;
if(m==n)
print(P,n);
else
for(j=m;j<=n;j++)
{
t=P;
P=P;
P=t;
perm1(m+1,n);
t=P;
P=P;
P=t;
}
}

main()
{
int j,n;
clrscr();
window(35,1,46,1);
textbackground(4);
textcolor(128);
clrscr();
printf("www.cndos.cn\n");
printf("Please input the value of n:");
scanf("%d",&n);
for(j=1;j<=n;j++)
P=j;
perm1(1,n);
printf("\nPress any key to end.");
getch();
}
Floor 10 Posted 2008-06-29 11:52 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
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 ]
批处理之家新域名:www.bathome.net
Floor 11 Posted 2008-06-29 13:37 ·  中国 陕西 移动(全省通用)
银牌会员
★★★★
钻石会员
Credits 2,278
Posts 1,020
Joined 2007-11-19 13:34
18-year member
UID 103127
Gender Male
Status Offline
```@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```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
xeibobin +2 2009-09-20 06:51
山外有山,人外有人;低调做人,努力做事。

进入网盘(各种工具)~~ 空间~~cmd学习
Floor 12 Posted 2008-06-29 13:47 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Finally improved a little bit of efficiency, local test permuting six-digit numbers takes nearly 10 minutes:

@echo off&setlocal enabledelayedexpansion
if exist pl.txt del /q pl.txt
:enter
cls&set /p codes=Please enter three or more characters (characters should be different and please separate with spaces):
if not defined codes goto enter
for %%i in (%codes%) do set /a m+=1
if %m% lss 3 set m=0&goto enter
cls&echo Arranging, please wait...
set /a num1=1,x=m,y=m-1
:lp
set /a num1*=m,m-=1
if %m% neq 0 goto lp
set /a num2=num1/6+1,sz=1
:begin
set code=%codes: =%&set str=&set n=%x%
:again
set /a a=%random%%%%n%,n-=1
set a=!code:~%a%,1!
set str=%str%%a%&set code=!code:%a%=!
if "!str:~%y%!" equ "" goto again
for /l %%a in (1,1,%sz%) do (
for %%i in (!var%%a!) do (
if "%%i" equ "%str%" goto begin

)
)
echo %str%>>temp.txt
set var%sz%=!var%sz%! %str%
set /a m+=1,z+=1
if %z% equ 6 set /a sz+=1,z=0
if %m% neq %num1% goto begin
sort temp.txt>pl.txt&del /q temp.txt&start pl.txt
批处理之家新域名:www.bathome.net
Floor 13 Posted 2008-06-29 14:07 ·  中国 陕西 移动(全省通用)
银牌会员
★★★★
钻石会员
Credits 2,278
Posts 1,020
Joined 2007-11-19 13:34
18-year member
UID 103127
Gender Male
Status Offline
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)
@echo off&setlocal enabledelayedexpansion
set s1=a b c e d f g
set t=%time%
for %%a in (!s1!)do (set s2=!s1:%%a=!
for %%b in (!s2!)do (set s3=!s2:%%b=!
for %%c in (!s3!)do (set s4=!s3:%%c=!
for %%d in (!s4!)do (set s5=!s4:%%d=!
for %%e in (!s5!)do (set s6=!s5:%%e=!
for %%f in (!s6!)do (set s7=!s6:%%f=!
for %%g in (!s7!)do (echo %%a%%b%%c%%d%%e%%f%%g
) ) ) ) ) ) )
call timediff.bat %t% %time% 0
pause
山外有山,人外有人;低调做人,努力做事。

进入网盘(各种工具)~~ 空间~~cmd学习
Floor 14 Posted 2008-06-29 16:33 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
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)
@echo off&setlocal enabledelayedexpansion
set s1=a b c e d f g
set t=%time%
f ...

The method is good, it's fast, but please clarify the original poster's intention: "The number of characters is not fixed".
批处理之家新域名:www.bathome.net
Floor 15 Posted 2008-06-29 17:19 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
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...
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
pusofalse +11 2008-12-18 12:39
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Forum Jump: