- @echo off&setlocal enabledelayedexpansion
- (set/p=初始化中, 请稍等...<nul)&(call :initRoots)
- echo.&echo cmd 下最大正整数 2147483647 (2^^31-1) 的各次方根整数部分(次数2--30):
- (for /l %%a in (2,1,30) do set /p=!root%%a!,<nul) & (echo )
- :loop
- set/p i=请输入正整数:
- if %i% lss 1 goto loop
- if %i% equ 1 echo 1的任意次方=1&goto loop
- set /a exp=1
- for /l %%a in (2,1,30) do if %i% leq !root%%a! set /a exp+=1
- echo %i%的%exp%次方接近于cmd最大值.
- goto loop
-
- :initRoots 初始化 cmd 下最大正整数 2147483647 (2^31-1) 的各次方根(次数2--30)
- for /l %%a in (2,1,30) do (call :searchRoot root%%a 2 50000 %%a)& set /p=^>^><nul
- exit /b
-
- :checkOverflow overflow x y rem 检测 x^y 是否溢出
- (set tc=%2)&(set te=%3)
- :next
- if !te! gtr 1 set /a "t0=tc,tc*=%2,t1=tc/ %2,te-=1"
- if !t0! neq !t1! (set %1=yes)&(exit /b)
- if !t0! geq !tc! (set %1=yes)&(exit /b)
- if !tc! leq 0 (set %1=yes)&(exit /b) else if !te! leq 1 (set %1=no)&(exit /b) else goto next
- exit /b
-
- :searchRoot root lower upper time
- set /a "ll=%2,uu=%3"
- :schNext
- set /a t=(ll+uu)/2
- if !t! equ !ll! set /a "%1=t" & exit /b
- call :checkOverflow ofl !t! %4
- if /i "!ofl!"=="yes" (set /a "uu=t") else (set /a "ll=t")
- goto schNext
- exit /b
复制代码
[ 本帖最后由 neorobin 于 2009-12-19 03:42 编辑 ] |