自己也照葫芦画瓢写了一个,满有趣的- @echo off
- rem 字符串长度上限;替换8为你想要的数值,但值必须是2的冪
- set /a nem=8
- rem 前面以2为底的冪的指数
- set /a lop=3
- setlocal enabledelayedexpansion
-
- :test
- cls
- set /p "str=输入字符串:"
- if not defined str echo. 输入为空...&goto test_end
- set /a max=%nem%,min=0
- echo max : min : length
- echo ( x + y ) / 2 = z
- echo.
- for /l %%a in (1,1,%lop%) do (
- set /a "length=(max+min)/2"
- echo ^(!max!^) + ^(!min!^) ==^> ^(!length!^)
- for /f "delims=" %%b in ("!length!") do (
- if "!str:~%%b!" equ "" (
- echo 完成折半,提取完成,字串第^(!length!+1^)位^(为^)空,继续逼近,设置max^(!max!^)为length^(!length!^)
- set /a max=length
- ) else (
- echo 完成折半,提取完成,字串第^(!length!+1^)位^(非^)空,开始远离,设置min^(!min!^)为length^(!length!^)
- set /a min=length
- ) )
- echo ^(!max!^) ^| ^(!min!^) ^<== ^(!length!^)&echo.
- )
- rem 如果提取字串不为空则+1
- if "!str:~%length%!" neq "" set /a length+=1&echo 不为空+1
- echo.
- rem 继续判断是否溢出
- if "!str:~%length%!" neq "" (echo. 字符串str超过%nem%个字符) else echo. 经计算字符串str共有!length!个字符
- :test_end
- pause>nul
- goto test
复制代码
|