Board logo

标题: [原创]******找最大公约数****** [打印本页]

作者: s11ss     时间: 2007-9-21 11:11    标题: [原创]******找最大公约数******

CODE:  [Copy to clipboard]
@echo off
setlocal enabledelayedexpansion
::::::::::::::::::::::::::::Find The Biggest Public Divisor::::::::::::::::::::::::::::
::::::::::::::::::::::::::::       {s11ss  2007-9-21}       ::::::::::::::::::::::::::::
:r
echo Please input two integers,like 9,21:
set /p n=
echo.
echo Calculating...
set /a i=1
for %%a in (!n!) do (
        set n!i!=%%a
        set /a i+=1
)
if !n1! equ !n2! (
        call :e !n1!
        goto :eof
)
if !n1! lss !n2! (
        set t=!n1!
        set n1=!n2!
        set n2=!t!
)
set /a big=!n1!
set /a small=!n2!
:c
set /a remain=!big!%%!small!
if !remain! equ 1 (
        echo.
        echo They don't have the public divisor.
        goto :e
)
if !remain! equ 0 (
        echo.
        echo The biggest public divisor is !small!.
        goto :e
) else (
        set big=!small!
        set small=!remain!
        goto :c
        )
:e
echo.
echo Press Any Key To Exit...
pause>nul

作者: youxi01     时间: 2007-9-21 13:42
LZ的代码似乎有些长,发一段代码,支持求若干个数字之间的公约数
CODE:  [Copy to clipboard]
@echo off 2>nul 3>nul& setlocal enabledelayedexpansion
echo 请输入几个整数,类似:21 7 14
set /p n=
set num2=%n:* =%
for %%i in (%n%) do call :test %%i !num2!
echo 它们的公约数为:%num2%

pause>nul
:test %num1% %num2%
set /a temp=%1 %% %2
if %temp% NEQ 0 (
      set/a num1=%2,num2=%temp%
      call :test !num1! !num2!) else set num2=%2
[ Last edited by youxi01 on 2007-9-21 at 01:43 PM ]




欢迎光临 中国DOS联盟论坛 (http://cndos.fam.cx/forum/) Powered by Discuz! 2.5