标题: 求助批处理生成不重复的3位字符 [打印本页]
作者: newgain 时间: 2023-9-6 02:04 标题: 求助批处理生成不重复的3位字符
求助生成不重复的1-4位字符(大小写字母、数字),并输出TXT文件;
文件可能会比较大,最好位数可调。win10系统。
感谢10元。
作者: Nsqs 时间: 2023-9-6 02:48
- powershell -noprofile -executionpolicy bypass "1..100|%%{((97..122),(65..90),(48..57)|%%{[char[]]$_}|Get-Random -Count 4) -join ''}" > rnd.txt
复制代码
生成100个随机字符,每行一组
作者: wanghan519 时间: 2023-9-6 05:27
本帖最后由 wanghan519 于 2023-9-6 05:30 编辑
awk写了个幂集,不知道对不对,可以存入文件a.awk再运行echo asdfg | awk -f a.awk试一下
只考虑了组合,没考虑排列。。。- function powerset(x, y) {
- if (length(x) > 3 || y > length($0)) {
- print x
- return
- }
- powerset(x, y + 1)
- powerset(x substr($0, y, 1), y + 1)
- }
- {
- powerset("", 1)
- }
复制代码
作者: zaqmlp 时间: 2023-9-6 14:30
- /*&cls&echo off&cd /d "%~dp0"
- set "length=3"
- set "count=100"
- cscript -nologo -e:jscript "%~f0" "%length%" "%count%" > "out.txt"
- pause
- exit
- */
- var len=WSH.Arguments(0).split(/[-,~]/),count=Number(WSH.Arguments(1));
- var charlist=[
- ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"],
- ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],
- ["0","1","2","3","4","5","6","7","8","9"]
- ];
-
- var r1=Number(len[0]);
- function getrandom(){
- if(len.length==2){r1=Math.floor(Math.random()*(Number(len[1])-Number(len[0]))+Number(len[0]));}
- var s=[];
- for(var i=1;i<=r1;i++){
- var r2=Math.floor(Math.random()*charlist.length);
- var r3=Math.floor(Math.random()*charlist[r2].length);
- s.push(charlist[r2][r3]);
- }
- return s.join("");
- }
-
- var dic={},n=0;
- for(;;){
- var rs=getrandom();
- if(dic[rs]==undefined){
- WSH.echo(rs);
- n=n+1;dic[rs]='';
- if(n>=count){break;}
- }
- }
复制代码
作者: newgain 时间: 2023-9-6 21:10
回复 4# zaqmlp
E:\DESK\输出.bat(10, 3) Microsoft JScript 编译错误: 缺少 ']'
作者: zaqmlp 时间: 2023-9-6 21:43
回复 5# newgain
核对代码,确保复制完整
作者: newgain 时间: 2023-9-7 00:09
回复 6# zaqmlp
作者: zaqmlp 时间: 2023-9-7 00:40
回复 5# newgain
不要手动选中复制,直接点击帖子自带的复制按钮
https://pan.baidu.com/s/1SIKi0VaD3Tlo9R3HJO0vcA?pwd=4t45
作者: newgain 时间: 2023-9-7 07:25
回复 8# zaqmlp
只输出100多个,这个数量不全
作者: newgain 时间: 2023-9-7 07:26
回复 8# zaqmlp
可以输出所有的么1~4位组合么
作者: wanghan519 时间: 2023-9-7 09:14
本帖最后由 wanghan519 于 2023-9-7 09:29 编辑
python最简单,因为有itertools,- import itertools
- s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
- with open(r'a.txt','w') as f:
- for i in range(1, 5):
- for j in itertools.permutations(s, i):
- f.write(''.join(j)+'\n')
复制代码
前两天刚提过的nushell可以方便的写sql,似乎sql写递归也很方便- with recursive c as (
- select x, 1 n from t
- union all
- select c.x || t.x, c.n + 1
- from c join t
- where c.n < 4 and c.x not like '%' || t.x || '%'
- )
- select x from c;
复制代码
或者就像上面那样再试试awk递归怎么写,上面那是列出所有组合,没考虑全排列
作者: qixiaobin0715 时间: 2023-9-7 09:28
本帖最后由 qixiaobin0715 于 2023-9-7 09:54 编辑
- @echo off
- set str1=0 1 2 3 4 5 6 7 8 9
- set str2=a b c d e f g h i j k l m n o p q r s t u v w x y z
- set str3=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
- set str=%str1% %str2% %str3%
- (for %%i in (%str%) do (
- echo,%%i
- for %%j in (%str%) do (
- echo,%%i%%j
- for %%k in (%str%) do (
- echo,%%i%%j%%k
- for %%l in (%str%) do (
- echo,%%i%%j%%k%%l
- )
- )
- )
- ))>1.txt
- pause
复制代码
作者: qixiaobin0715 时间: 2023-9-7 09:56
回复 1# newgain
12楼代码发上来有笔误,现已修改。
作者: zaqmlp 时间: 2023-9-7 13:57
回复 10# newgain - <# :
- cls&echo off&cd /d "%~dp0"&set "current=%cd%"
- path %SYSTEMROOT%\System32\WindowsPowerShell\v1.0;%path%
- echo;waiting...
- powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal \"%~f0\"|Out-String|Invoke-Expression"
- echo;done
- pause
- exit
- #>
- $current=($env:current).trimend('\');
- $outfile=$current+'\out.txt';
- $fs=New-Object System.IO.FileStream($outfile, [System.IO.FileMode]::Create);
- $sw=New-Object System.IO.StreamWriter($fs, [Text.Encoding]::GetEncoding('GB2312'));
- $arr=New-Object -TypeName System.Collections.ArrayList;
- $charlist=@(97..122)+@(65..90)+@(48..57);
- for($i=1;$i -le 4;$i++){
- $brr=New-Object -TypeName System.Collections.ArrayList;
- if($arr.count -eq 0){
- for($j=0;$j -lt $charlist.length;$j++){
- $c=([char]$charlist[$j]).toString();
- $sw.WriteLine($c);
- [void]$brr.add($c);
- }
- }else{
- for($j=0;$j -lt $arr.count;$j++){
- for($k=0;$k -lt $charlist.length;$k++){
- $c=([char]$charlist[$k]).toString();
- $s=$arr[$j]+$c
- $sw.WriteLine($s);
- [void]$brr.add($s);
- }
- }
- }
- $sw.Flush();
- $arr=$brr;
- }
- $sw.Close();
- $fs.Close();
复制代码
作者: Nsqs 时间: 2023-9-7 14:39
我大PowerShell没资格参与这问题?
作者: newgain 时间: 2023-9-9 08:22
回复 14# zaqmlp
找不到输出的文件在哪
作者: newgain 时间: 2023-9-9 08:23
回复 13# qixiaobin0715
谢谢,发我一下微信收款码
作者: qixiaobin0715 时间: 2023-9-9 08:57
回复 17# newgain
不麻烦了,算是免费赠送好了。
作者: zaqmlp 时间: 2023-9-9 11:36
回复 16# newgain
out.txt
代码里有写,你不会看一下?或者放到一个空文件夹里运行
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |