文、代码:523066680
blog: http://hi.baidu.com/523066680
发表于:http://www.cn-dos.net/forum/viewthread.php?tid=41243&fpage=1&highlight=&page=2 27楼
先贴代码
@echo off&setlocal enabledelayedexpansion
title code by hi.baidu.com/523066680
errorcommand>list.x 2>nul
echo,允许字符串重复,字符数不定,空格什么的字符就别试了哈. &echo.
set /p str="输入将被枚举排列情况的字符串(形式如:abcd): "
call :fo "%str%" ""
echo,--------已将信息导入 list.x文件 请检查.
pause
exit
:fo
if %1=="" (echo,%~2 &echo,%~2>>list.x&goto :eof)
if not defined _%~1 (set _%~1=-1)
set str=%~1
:foa
set /a _%~1+=1,foa=_%~1,fob=foa+1
call :fo "!str:~0,%foa%!!str:~%fob%!" "%~2!str:~%foa%,1!"
set str=%~1
set /a foa=_%~1
if not "!str:~%foa%,-1!"=="" (goto :foa)
set "_%~1="
goto :eof
我来了,黑板报出完了,心情无比放松,看中这道题,"研究"了几天,有点结果了,分享下.
主要内容 1.谈谈前面楼的代码
2.分享我在解题过程中学到的东西
首先谈谈楼上的内容
以 plp626 提出的多重for最为快速,虽然只能对指定数量的字符,但是可以修改啊,
22楼修改的不错,我觉得能到10个字符就可以满足需求了,尤其是速度就像直接type答案一样快.
但是作为一个学者,寻求更好的办法是有必要的.于是23楼的代码超短(Good!),且符合要求
虽然没来得及理解,暂时发现的问题是:
当我输入6个字符时,结束的时候按Enter居然不退出,估计
可能死循环了.又看了下,汗,请大家测试的时候在pause>nul后面加exit
另外,输入字符数多运行时间长是很自然的事情,我觉得不用挑这个毛病.
本人认为23楼代码很短,精悍,值得学习下的.
下面先说说本人解题过程学到的东西:
主要是想要讨论call 的参数递归,关于这道题 ,上次看到batman 有快速的解法,
我就不转载了 期待 batman 大哥 续帖]
1.也想对plp626代码改进,玩了一下用%var% 代表 一条命令执行的方法,下面给出理解版:
@echo off
set fo="for %%1 in (a b c) do (for %%2 in (a b c) do ("
set end="))"
%fo:"=%
echo %%1 %%2
%end:"=%
pause
感觉太好玩了,不够"自动化"对吧?ok 我们用for 来决定要用几重for ,曾试着重组那个多重For的思路,
不过变量跟符号太多了,实在搞不下去,停工,思路别浪费,做别的. 用这个思路实现:
根据用户输入的符号,列出这些符号包括本身的所有组合
这是个简单点的题目,例如输入 1 2 则输出 1 1 , 1 2 , 2 1 , 2 2 好处主要是允许输入字符数不定.
虽然不了解"密码字典",不过感觉上要做的话这个代码应该更适合(比如说密码是223456的时候).
代码如下:
@echo off &setlocal enabledelayedexpansion
title code by hi.baidu.com/523066680
set "fo=" &set "end=" &set "n=0"
set /p "str=输入字符,以空格隔开: "
for %%a in (%str%) do set /a n+=1
for /l %%x in (1,1,%n%) do (
set fo="for %%%%x in (%str%) do (!fo!"
set end="!end!)"
set echo=!echo! %%%%x
)
%fo:"=%
echo %echo%
%end:"=%
pause
2.call 参数 回归
实现这个,我觉得可能Call可以实现,记得call
返回的时候,接下来的东西都是原来的,会不会参数也是原来的?
所以,不能期待set 的变量也能有"重叠的记忆功能" ,要么加上编号,但会很繁杂的.
-----------------以下内容谈论call 参数
废话说多了,开始探究call有多强大(call 参数返回 理解版):
@echo off
call :a 123
:a
echo %1
:b
if %1==123 (call :a 456) else (goto :eof)
echo %1
pause
exit
还算好理解吧? 来个多层的,玩转call:
@echo off
echo,call :fo "abcd"
call :fo "abcd"
pause
exit
:fo
if %1=="" (echo,参数1已经没有字符了,返回call,接下一句 &goto :eof)
set str=%~1
:foa
echo,call :fo "%str:~1%"
call :fo "%str:~1%"
echo 已返回,现参数1的值为 %~1
goto :eof
::一定很郁闷对吧,后面一句echo 咋显示4次,奥妙尽在goto :eof 因为本身就是被call了多次啊.
越玩越有趣啊, 自己那个代码就不打算详细分析了,写这篇文章不是晕大家的,所以我也主要用中文描述,讲一些可能用的上的东西.
温馨提示:很多问题阻碍了我很久,而我最后一次理清所有思路,是在关上电脑的时候,可见:经常对着电脑不利于思考啊.
----------------末
本人知识及能力实在有限,感觉这样分层的call并分层的返回 应该就是前面楼说的 "递归" 了吧
希望有资料的人把相关资料分享下哈. 现在感觉像是进入了完全不知道的逻辑领域....
Last edited by 523066680 on 2008-12-19 at 10:41 ]
Article, code: 523066680
blog: http://hi.baidu.com/523066680
Posted at: http://www.cn-dos.net/forum/viewthread.php?tid=41243&fpage=1&highlight=&page=2 Floor 27
First, post the code
@echo off&setlocal enabledelayedexpansion
title code by hi.baidu.com/523066680
errorcommand>list.x 2>nul
echo,Allow string repetition, the number of characters is indefinite, don't try characters like spaces, etc. &echo.
set /p str="Enter the string whose permutation conditions will be enumerated (in the form like: abcd): "
call :fo "%str%" ""
echo,--------The information has been imported into the list.x file, please check.
pause
exit
:fo
if %1=="" (echo,%~2 &echo,%~2>>list.x&goto :eof)
if not defined _%~1 (set _%~1=-1)
set str=%~1
:foa
set /a _%~1+=1,foa=_%~1,fob=foa+1
call :fo "!str:~0,%foa%!!str:~%fob%!" "%~2!str:~%foa%,1!"
set str=%~1
set /a foa=_%~1
if not "!str:~%foa%,-1!"=="" (goto :foa)
set "_%~1="
goto :eof
I'm here. The blackboard newspaper is finished. I'm extremely relaxed. I saw this problem and "studied" it for a few days. I have some results and want to share them.
Main content: 1. Talk about the previous floor's code
2. Share what I learned in the process of solving the problem
First, talk about the content of the previous floor
The multiple for proposed by plp626 is the fastest. Although it can only handle a specified number of characters, it can be modified. The modification by floor 22 is good. I think it can meet the needs when there are up to 10 characters, especially the speed is as fast as directly typing the answer.
But as a scholar, it is necessary to seek a better way. So the code on floor 23 is very short (Good!), and meets the requirements. Although I didn't have time to understand it, the temporary problem found is:
When I enter 6 characters, when I press Enter at the end, it doesn't exit. I guess
Maybe there is a dead loop. Looking at it again, oh, sweat. Please add exit after pause>nul when testing.
In addition, it is natural that the running time is long when the number of input characters is large. I don't think this should be picked on.
I think the code on floor 23 is very short and concise, worthy of learning.
Next, first talk about what I learned in the process of solving the problem:
Mainly wanting to discuss the recursive call of parameters. Regarding this problem, I saw Batman's fast solution last time,
I won't reprint it. Looking forward to Brother Batman's follow-up post]
1. Also want to improve the plp626 code. I played with the method of using %var% to represent a command execution. The following is the understanding version:
@echo off
set fo="for %%1 in (a b c) do (for %%2 in (a b c) do ("
set end="))"
%fo:"=%
echo %%1 %%2
%end:"=%
pause
It feels too fun, not "automated" enough, right? OK, we use for to determine how many layers of for to use. I once tried to reorganize the idea of multiple For,
But there are too many variables and symbols, it's really impossible to do, stop working, don't waste the idea, do something else. Use this idea to realize:
List all combinations including itself of these symbols according to the symbols input by the user
This is a simpler topic. For example, if you enter 1 2, then output 1 1, 1 2, 2 1, 2 2. The advantage is mainly that the number of input characters is indefinite.
Although I don't understand "password dictionary", but it feels that this code should be more suitable (for example, when the password is 223456).
The code is as follows:
@echo off &setlocal enabledelayedexpansion
title code by hi.baidu.com/523066680
set "fo=" &set "end=" &set "n=0"
set /p "str=Enter characters, separated by spaces: "
for %%a in (%str%) do set /a n+=1
for /l %%x in (1,1,%n%) do (
set fo="for %%%%x in (%str%) do (!fo!"
set end="!end!)"
set echo=!echo! %%%%x
)
%fo:"=%
echo %echo%
%end:"=%
pause
2. Return of call parameters
I think this may be achievable with Call. I remember that when Call returns, the following things are all original. Will the parameters also be original?
So, don't expect the variables of set to have "overlapping memory functions", either add numbers, but it will be very complicated.
-----------------The following content talks about call parameters
Talked too much nonsense, start to explore how powerful call is (understanding version of call parameter return):
@echo off
call :a 123
:a
echo %1
:b
if %1==123 (call :a 456) else (goto :eof)
echo %1
pause
exit
It's still easy to understand? Here's a multi-layer one, play with call:
@echo off
echo,call :fo "abcd"
call :fo "abcd"
pause
exit
:fo
if %1=="" (echo,Parameter 1 has no more characters, return call, next sentence &goto :eof)
set str=%~1
:foa
echo,call :fo "%str:~1%"
call :fo "%str:~1%"
echo Returned, now the value of parameter 1 is %~1
goto :eof
::Must be very depressed, right? Why does the following echo show 4 times? The mystery is all in goto :eof because it was called multiple times itself.
It's getting more and more interesting to play. I don't plan to analyze my own code in detail. This article is not to confuse everyone, so I mainly use Chinese to describe it and talk about some things that may be useful.
Friendly reminder: Many problems have troubled me for a long time, and the last time I sorted out all the ideas was when I closed the computer. It can be seen: It's not good for thinking to face the computer often.
----------------End
My knowledge and ability are really limited. I feel that this layered call and layered return should be the "recursion" mentioned in the previous floor.
I hope those who have information can share the relevant materials. Now I feel like I have entered a completely unknown logical field....
Last edited by 523066680 on 2008-12-19 at 10:41 ]