标题: [文本处理] [已解决]批处理怎样把一组数据排列组合? [打印本页]
作者: wxyz0001 时间: 2019-10-27 18:20 标题: [已解决]批处理怎样把一组数据排列组合?
有一组数据:[01,02,04,08,09,12,13,14,15,19,20,22,23,25,28,30,31,34,35]
每5个数字一行,
中间用逗号隔开,
每行数据不同,
还要排序
用XP批处理代码编写>>a.txt
格式如下:
01,02,04,08,09
01,02,04,08,12
01,02,04,08,13
......
01,02,04,08,35
01,02,04,09,12
01,02,04,09,13
.......
var[01,02,04,08,09,12,13,14,15,19,20,22,23,25,28,30,31,34,35]
参照@terse写的代码
________________________________________________- @echo off&setlocal enabledelayedexpansion
- set " var=01 02 03 04 05 06 07 08 09 10"
- set "m=5"
- for %%i in (%var%) do (
- set /a n+=1&set #!n!=%%i
- if !n! leq !m! (set s=1!s!)else set s=!s!0
- )
- set/a r=n-m,i=j=1
- for /l %%i in (1,1,%r%) do set /a "i*=(n-%%i+1),j*=%%i"
- set s=0!s!&set /a "i/=j"
- (for /l %%i in (1 1 !i!) do (
- for /l %%j in (1 1 !n!) do (
- if "!s:~%%j,1!" == "1" set $=!$!,!#%%j!
- )
- for /f %%a in ("!s:10= !") do set "j=%%a"
- set s=0!j:0=!!j:1=!1!s:*10=!
- echo!$!&set "$="
- ))>$#.t
- sort $#.t /o a.txt
- del $#.t
- pause
复制代码
________________________________________________
排列组合公式:
set /a "i*=(n-%%i+1),j*=%%i"
C [10,5] =10*9*8*7*6/(5*4*3*2*1)=252
set /a "i/=j"
i=i(30240)/j(120)
i=252
作者: terse 时间: 2019-10-27 19:04
明确的说 就是 5选4 然后 FOR
代码前面已经给了吧
作者: ivor 时间: 2019-10-27 20:25
python版本的- import itertools
-
- for i in list(itertools.permutations(['01','02','04','08','09','12','13','14','15','19','20','22','23','25','28','30','31','34','35'],5)):
- print(i)
复制代码
1395360种组合
作者: Batcher 时间: 2019-10-28 14:37
- @echo off
- setlocal enabledelayedexpansion
- set "var=01 02 03 04 05 06 07 08 09 10"
- set "m=5"
- for %%i in (%var%) do (
- set /a n+=1
- set #!n!=%%i
- if !n! leq !m! (
- set s=1!s!
- ) else (
- set s=!s!0
- )
- echo --- 正在处理的var值:%%i
- REM n没有初值,从0开始,每次循环加1
- echo 当前n的值:!n!
- REM s没有初值,从空字符串开始,每次循环在左边插入1或者在右边插入0
- echo 当前s的值:!s!
- )
- pause
复制代码
作者: wxyz0001 时间: 2019-10-28 14:48
回复 4# Batcher
谢谢
作者: wxyz0001 时间: 2019-10-28 14:50
回复 3# ivor
谢谢回复,我不会python,看不懂,暂时不学python
作者: Batcher 时间: 2019-10-30 09:25
回复 5# wxyz0001
如果不知道j的值是多少,观察一下:- @echo off
- setlocal enabledelayedexpansion
- set "var=01 02 03 04 05 06 07 08 09 10"
- set "m=5"
- for %%i in (%var%) do (
- set /a n+=1
- set #!n!=%%i
- if !n! leq !m! (
- set s=1!s!
- ) else (
- set s=!s!0
- )
- echo --- 正在处理的var值:%%i
- REM n没有初值,从0开始,每次循环加1
- echo 当前n的值:!n!
- REM s没有初值,从空字符串开始,每次循环在左边插入1或者在右边插入0
- echo 当前s的值:!s!
- )
- echo,
- set /a r=n-m,i=j=1
- for /l %%i in (1,1,%r%) do (
- set /a "i*=(n-%%i+1)"
- set /a "j*=%%i"
- )
- set s=0!s!
- set /a "i/=j"
- (for /l %%i in (1 1 !i!) do (
- for /l %%j in (1 1 !n!) do (
- if "!s:~%%j,1!" == "1" (
- set $=!$!,!#%%j!
- )
- )
- for /f %%a in ("!s:10= !") do (
- set "j=%%a"
- )
- echo j的值是:!j!
- echo 替换之前s的值是:!s!
- set s=0!j:0=!!j:1=!1!s:*10=!
- echo 替换之后s的值是:!s!
- echo !$!
- set "$="
- pause
- ))
- REM >$#.t
- REM sort $#.t /o a.txt
- REM del $#.t
- REM pause
复制代码
作者: wxyz0001 时间: 2021-5-31 14:12
python版本的1395360种组合
ivor 发表于 2019-10-27 20:25
你的这个库用起来真方便
我没用库写的,代码可能还不够优化- # 如果x小于10,前面加0
- def zero(x):
- if x < 10:
- return '0' + str(x)
- else:
- return str(x)
-
- # 相邻的数不重复
- def matrix(i, j, k, m, n):
- if ((i != j) and (i != k) and (i != m) and (i != n) \
- and (j != k) and (j != m) and (j != n) \
- and (k != m) and (k != n) and (m != n)):
- return i, j, k, m, n
-
- # 输出矩阵
- lottery = [3, 4, 6, 8, 9, 10, 13, 15, 16, 18]
- for a in lottery:
- for b in lottery:
- for c in lottery:
- for d in lottery:
- for e in lottery:
- if matrix(a, b, c, d, e) != None:
- print(zero(a), zero(b), zero(c), zero(d), zero(e))
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |