|
quya
高级用户
    五星老土
积分 558
发帖 172
注册 2003-2-9 来自 江苏
状态 离线
|
『第 61 楼』:
使用 LLM 解释/回答一下
Originally posted by zw19750516 at 2008-7-14 10:39 AM:
效率问题出在这里:echo %input%|findstr >nul||set str=#%str:~1,-1%%#
在批处理中要尽量用if做判断,少用findstr。
如前所述, 这是为了兼容性. 我最得意的就是这句了. 而且实际上这里只存在2个字母的判断, 你的CODE再长, 也依旧是2个字母.
高效率和简洁应该建立在准确的前提上吧?
比较了38楼的代码, 运行速度快我很多, 我承认, 因为几乎在瞬间给出答案. 但正如作者所言无法处理重复字母.
我的57楼代码需要等待一下. 想改进但囿于水平问题, 无法再提高了.
另外, 谁能告诉我57楼第二段代码错在哪里? 谢谢了
Last edited by quya on 2008-7-14 at 11:20 AM ]
Originally posted by zw19750516 at 2008-7-14 10:39 AM:
The efficiency problem lies here: echo %input%|findstr >nul||set str=#%str:~1,-1%%#
In batch processing, try to use if for judgment as much as possible and use findstr less.
As mentioned earlier, this is for compatibility. What I'm most proud of is this sentence. And in fact, there is only a judgment of 2 letters here. No matter how long your CODE is, it's still 2 letters.
High efficiency and conciseness should be based on accuracy, right?
Compared with the code on floor 38, it runs much faster, and I admit it because it gives the answer almost instantly. But as the author said, it can't handle repeated letters.
My code on floor 57 needs to wait a bit. I want to improve it but due to the level problem, I can't improve it anymore.
Also, who can tell me what's wrong with the second paragraph of the code on floor 57? Thanks
Last edited by quya on 2008-7-14 at 11:20 AM ]
|

我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已. |
|
2008-7-14 10:48 |
|
|
quya
高级用户
    五星老土
积分 558
发帖 172
注册 2003-2-9 来自 江苏
状态 离线
|
 『第 62 楼』:
使用 LLM 解释/回答一下
Originally posted by zw19750516 at 2008-7-14 08:38 AM:
老土,代码第一是考虑效率,第二才是简洁,你这个方案存在效率问题,同时('echo %str%')可以修改为("%str%")。
考虑了下, 不得不面对效率不高的问题, 所以修改如下:
@echo off
set/p input=请输入:
set string=0123456789abcdefghij
:pro1
set str=%input:~0,1%
set input=%input:~1%
call set string=%%string:%str%=#%%
if defined input goto pro1
set "str= "
:pro2
set str=%string:~-1%%str%%string:~0,1%
if %string:~0,1%==# set str=#%str:~1,-1%%#
if %string:~-1%==# set str=#%str:~1,-1%%#
set string=%string:~1,-1%
if not defined string goto end
goto pro2
:end
for /f "tokens=1,2 delims= " %%i in ("%str%") do echo %%j ^| %%i
pause
这下速度同样飞快了.
Originally posted by zw19750516 at 2008-7-14 08:38 AM:
Old-fashioned, the first consideration in code is efficiency, and the second is conciseness. Your solution has an efficiency issue, and ('echo %str%') can be modified to ("%str%").
After considering it, I have to face the problem of low efficiency, so the modification is as follows:
@echo off
set/p input=Please enter:
set string=0123456789abcdefghij
:pro1
set str=%input:~0,1%
set input=%input:~1%
call set string=%%string:%str%=#%%
if defined input goto pro1
set "str= "
:pro2
set str=%string:~-1%%str%%string:~0,1%
if %string:~0,1%==# set str=#%str:~1,-1%%#
if %string:~-1%==# set str=#%str:~1,-1%%#
set string=%string:~1,-1%
if not defined string goto end
goto pro2
:end
for /f "tokens=1,2 delims= " %%i in ("%str%") do echo %%j ^| %%i
pause
Now the speed is also very fast.
|

我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已. |
|
2008-7-14 13:00 |
|
|
bat-zw
金牌会员
      永远的学习者
积分 3105
发帖 1276
注册 2008-3-8
状态 离线
|
『第 63 楼』:
使用 LLM 解释/回答一下
还有个问题要指出一下:
if 判断语句的最好加上引号 if "%string:~0,1%"=="#" 或 if "%string:~0,1%" equ "#"
There is another problem to point out:
The if judgment statement had better add quotes. if "%string:~0,1%"=="#" or if "%string:~0,1%" equ "#"
|

批处理之家新域名:www.bathome.net |
|
2008-7-14 16:08 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 64 楼』:
使用 LLM 解释/回答一下
再精简一下,思路更加清晰
@echo off
set/p input=请输入:
set tab=012t456789abcd6fghi3
:pro1
call set tab=%%tab:%input:~0,1%=#%%
set input=%input:~1%
if defined input goto pro1
set "out= "
:pro2
set out=#%out%#
if not "%tab:~0,1%"=="#" if not "%tab:~-1%"=="#" set out=%tab:~-1%%out:~1,-1%%tab:~0,1%
set tab=%tab:~1,-1%
if defined tab goto pro2
for /f "tokens=1,2 delims= " %%i in ("%out%") do echo %%j ^| %%i
pause
Simplify further, making the idea clearer
@echo off
set/p input=Please enter:
set tab=012t456789abcd6fghi3
:pro1
call set tab=%%tab:%input:~0,1%=#%%
set input=%input:~1%
if defined input goto pro1
set "out= "
:pro2
set out=#%out%#
if not "%tab:~0,1%"=="#" if not "%tab:~-1%"=="#" set out=%tab:~-1%%out:~1,-1%%tab:~0,1%
set tab=%tab:~1,-1%
if defined tab goto pro2
for /f "tokens=1,2 delims= " %%i in ("%out%") do echo %%j ^| %%i
pause
|
|
2008-7-14 18:34 |
|
|
terse
银牌会员
    
积分 2404
发帖 946
注册 2005-9-8
状态 离线
|
『第 65 楼』:
使用 LLM 解释/回答一下
Originally posted by qzwqzw at 2008-7-14 18:34:
再精简一下,思路更加清晰
@echo off
set/p input=请输入:
set tab=012t456789abcd6fghi3
:pro1
call set tab=%%tab:%input:~0,1%=#%%
set input=%input:~1%
if defined input ...
怎么显示输入的变成#
我改一下
@echo off
setlocal enabledelayedexpansion
set/p input=请输入:
set tab=012t456789abcd6fghi3
set str=%tab%
set /a n-=1,m=0
:pro1
call set tab=%%tab:%input:~0,1%=#%%
set input=%input:~1%
if defined input goto pro1
:pro2
set/a n+=1,m-=1
if "!tab:~,1!"=="#" (set var1=!var1!!str:~%n%,1!&set var2=!str:~%m%,1!!var2!)else (
if "!tab:~-1!"=="#" (set var1=!var1!!str:~%n%,1!&set var2=!str:~%m%,1!!var2!)else set var1=%var1%#&set var2=#%var2%)
set tab=%tab:~1,-1%
if defined tab goto pro2
echo %var1% ^| %var2%
pause
Originally posted by qzwqzw at 2008-7-14 18:34:
Simplify it further, the idea is clearer
@echo off
set/p input=Please enter:
set tab=012t456789abcd6fghi3
:pro1
call set tab=%%tab:%input:~0,1%=#%%
set input=%input:~1%
if defined input ... How to display the input changed to #
I'll make a change
@echo off
setlocal enabledelayedexpansion
set/p input=Please enter:
set tab=012t456789abcd6fghi3
set str=%tab%
set /a n-=1,m=0
:pro1
call set tab=%%tab:%input:~0,1%=#%%
set input=%input:~1%
if defined input goto pro1
:pro2
set/a n+=1,m-=1
if "!tab:~,1!"=="#" (set var1=!var1!!str:~%n%,1!&set var2=!str:~%m%,1!!var2!)else (
if "!tab:~-1!"=="#" (set var1=!var1!!str:~%n%,1!&set var2=!str:~%m%,1!!var2!)else set var1=%var1%#&set var2=#%var2%)
set tab=%tab:~1,-1%
if defined tab goto pro2
echo %var1% ^| %var2%
pause
|

简单!简单!再简单! |
|
2008-7-14 23:58 |
|
|