å³æå¤æ°å±é
forã¨ifã使ããããã«ãªã£ãã®ã§ãProblem 1ãé¡æã©ããã«æ¸ããã¨ãã§ãã¾ãããã ããããããã¡ã¤ã«ã«ã¯andãorã¯ãªãã®ã§ãããã¯ifã2ã¤ä½¿ããã¨ã«ãªãã¾ãã
@echo off set /a N = 1000 set /a M = %N% - 1 set /a s = 0 for /L %%i in (1, 1, %M%) do ( set /a r3 = %%i %% 3 set /a r5 = %%i %% 5 if %r3% == 0 ( set /a s += %%i ) else ( if %r5% == 0 ( set /a s += %%i ) ) ) echo %s%
ããã§ã3 %% 2ã ã¨3ã2ã§å²ã£ãã¨ãã®ä½ãã§ãããããå®è¡ããã¨ã
( ã®ä½¿ãæ¹ã誤ã£ã¦ãã¾ãã
ããããªã«ããã¨ã©ã¼ã¡ãã»ã¼ã¸ã®ãããªãã®ãåºã¦ãã¾ããããããã©ãã§åºã¦ããã®ããããããã¾ãããããããã¨ãã¯å é è¡ã
@echo on
ã¨ããã¨åå ãããããã¨ãããã¾ãã
>euler001d.bat >set /a N = 1000 >set /a M = 1000 - 1 >set /a s = 0 ( ã®ä½¿ãæ¹ã誤ã£ã¦ãã¾ãã > if == 0 (
ã©ãããã
if %r3% == 0 (
ã®é¨åã®ããã§ããr3ã«ãªã«ãã»ããããã¦ããªããã§ããããªããããªããã¨ããã¨ãifãforã®ä¸ã®ç°å¢å¤æ°ã¯ããããå®è¡ãããåã«è©ä¾¡ãããããã§ãããã®å ´åã§ã¯ãforã®ä¸ã«setã³ãã³ãã§r3ã«å¤ãã»ãããã¦ããããã«è¦ãã¾ããããã®åã«r3ãè©ä¾¡ããã¦ãã¾ãã®ã§ããã®æç¹ã§ã¯r3ã«ã¯ä½ãã»ããããã¦ããªããã¨ã«ãªãã®ã§ããforãifã¯ãã®ãããªãã¨ããã°ãã°èµ·ããã®ã§åä»ã§ãã
goto
ãã®ãããªå ´åã§ä¸çªãæ軽ãªã®ã¯gotoã§ã«ã¼ããåããã¨ã§ãã
@echo off set /a N = 1000 set /a s = 0 set /a i = 1 :loop set /a r3 = %i% %% 3 set /a r5 = %i% %% 5 if %r3% == 0 ( set /a s += %i% ) else ( if %r5% == 0 ( set /a s += %i% ) ) set /a i += 1 if %i% LSS %N% goto :loop echo %s%
åã«goto :labelã§:labelã«é£ã¶ã¨ããã ãã§ãã
åã³for
ã©ããã¦ãforã使ãããã£ãããforã®ä¸ã§callãã¦ãcallå ã§sã«è¶³ãã®ã§ããããã
@echo off set /a N = 1000 set /a M = N - 1 set /a s = 0 for /L %%i in (1, 1, %M%) do call :sum %%i echo %s% exit /b 0 :sum call :is_valid %1 if %ERRORLEVEL% == 1 set /a s += %1 exit /b 0 :is_valid setlocal set /a r3 = %1 %% 3 if %r3% == 0 exit /b 1 set /a r5 = %1 %% 5 if %r5% == 0 exit /b 1 exit /b 0