【问题标题】:Is there a BATCH/CMD command that checks if a number is a perfect square? [closed]是否有一个 BATCH/CMD 命令来检查一个数字是否是一个完美的正方形? [关闭]
【发布时间】:2016-06-15 21:09:56
【问题描述】:

我正在尝试在批处理中编写一个程序,将数字转换为简化的激进形式。有没有办法检查一个数字是否是一个完美的正方形?

【问题讨论】:

  • 别再批量写了,还有很多不错的选择
  • 翻译:“我不知道如何批量解决这个问题,所以你需要学习另一种语言。” -- Marged
  • @rojo:很棒的评论! +oo

标签: batch-file cmd perfect-square


【解决方案1】:

下面的纯批处理文件解决方案,出现在this question,如果给定的数字不是一,也会得到最接近的完美正方形。

@echo off
setlocal

cls
set /P "N=Enter a number: "

set /A "x=N/(11*1024)+40, x=(N/x+x)>>1, x=(N/x+x)>>1, x=(N/x+x)>>1, x=(N/x+x)>>1, x=(N/x+x)>>1, x+=(N-x*x)>>31, M=x*x"

if %N% equ %M% (
   echo %N% is perfect square
   goto :EOF
)

set /A "I=(x+1)*(x+1), ID=I-N, MD=N-M"
if %ID% lss %MD% set M=%I%
echo The closest perfect square is %M%

【讨论】:

    【解决方案2】:

    最简单的方法是使用 PowerShell 命令。

    @echo off
    setlocal
    
    call :isSquare 25 && (
        echo The square root is an integer.
    ) || (
        echo The square root is a float.
    )
    
    goto :EOF
    
    :isSquare <num>
    for /f "tokens=2 delims=." %%I in ('powershell "[math]::Sqrt(%1)"') do exit /b 1
    exit /b 0
    

    虽然它比 npocmaka 的 JScript 宏慢。


    这是一个基于algorithm found on Wikipedia 的纯批处理解决方案。 (有关详细信息,请参阅标有“二进制数字系统(基数 2)”的部分。)如果数字是完全平方,:sqrt 函数设置错误级别 0,如果不是,则设置非零;并为结果设置一个变量。速度挺快的。

    @echo off
    setlocal
    
    set num=2147395600
    
    call :sqrt %num% foo && (
        call echo The square root of %num% is %%foo%%
    ) || (
        echo %num% is not a perfect square
    )
    
    goto :EOF
    
    :sqrt <num> <return_var>
    setlocal enabledelayedexpansion
    set /a "res = 0, bit = 1 << 30, num = %~1"
    
    :sqrt_loop1
    if %bit% gtr %num% (
        set /a "bit >>= 2"
        goto :sqrt_loop1
    )
    
    :sqrt_loop2
    if %bit% neq 0 (
        set /a resbit = res + bit
        if %num% geq !resbit! (
            set /a "num -= resbit, res >>= 1, res += bit"
        ) else set /a "res >>= 1"
        set /a "bit >>= 2"
        goto sqrt_loop2
    )
    
    set /a "ret = %~1 - (res * res)"
    endlocal & set "%~2=%res%" & exit /b %ret%
    

    【讨论】:

      【解决方案3】:

      最简单的方法是使用嵌入的 jscript 代码。这里的示例带有一个接受数字的子例程,根据数字是否为正方形打印是或否,如果是,则将 errorlevel 设置为 1:

      @echo off
      
      call :isSquare 81
      call :isSquare 7
      call :isSquare 9
      
      if errorlevel 1 (
          echo 9 is a square number
      )
      exit /b %errorlevel%
      
      
      :isSquare
      setlocal
      set /a number=%~1
      
      :: Define simple macros to support JavaScript within batch
      set "beginJS=mshta "javascript:code(close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write("
      set "endJS=)));""
      
      
      
      :: FOR /F does not need pipe
      for /f %%N in (
        '%beginJS% Math.sqrt(%number%) %endJS%'
      ) do set sqrt=%%N
      
      if "%sqrt%" equ "%sqrt:.=%" (
          echo Yep!
          endlocal & exit /b 1
      
      ) else (
          echo Nope!
          endlocal & exit /b 0
      )
      
      endlocal
      

      现在我正在考虑纯批处理解决方案(可能正在检查一个包含所有 34 位平方数的列表?)

      【讨论】:

      • 用纯批处理解决方案打败你。 :)
      猜你喜欢
      • 2015-11-06
      • 2016-07-03
      • 2017-01-09
      • 2011-09-27
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多