batch-file - 在批处理脚本中执行模算术

标签 batch-file

作为新事物,我正在尝试使用批处理脚本 ( https://projecteuler.net/problem=5 ) 完成 Project Euler Problem 5。然而;我遇到了一些问题。如果有人可以查看我的代码,那就太好了。

@ECHO off

SET init=1
SET iter=1
SET /a func=%init% %% %iter%
cls

:Num
IF func==0 (
    IF iter==20 (
        ECHO Val = %init%
        pause
        exit
    ) ELSE (
        SET /a iter+=1
        GOTO Num
    )
) ELSE (
    SET iter=1
    SET /a init+=1
    GOTO Num
)

它的意思是检查 init mod iter 是否返回 0,如果是,则在 iter 上加 1值,直到达到 21。但是;如果不等于 0,则迭代计数将重新设置为 0 并重新开始计算。

将要发生的事情的示例:
1 mod 1 = 0, Therefor add 1 to iter
1 mod 2 != 0, Therefor init is set to 0 and 1 is added to init
2 mod 1 = 0, Therefor add 1 to iter
2 mod 2 = 0, Therefor add 1 to iter
2 mod 3 != 0, Therefor init is set to 0 and 1 is added to init

等等等等。

发生了什么的一个例子:
1 mod 1 != 0, Therefor add 1 to init
2 mod 1 != 0, Therefor add 1 to init
3 mod 1 != 0, Therefor add 1 to init

等等等等。

任何帮助表示赞赏,谢谢。

最佳答案

这个怎么样:

@Echo off
setlocal enabledelayedexpansion
SET init=1 
SET iter=1 
cls 
set loopCounter=1
set loopBatch=1

:numLoop
SET /a func="!init! %% !iter!" 
IF !iter! == 21 (goto :done)
IF !func! == 0 (call :incIter) ELSE ( call :incInit)
SET /a loopCounter+=1 
SET /a loopBatch="%loopCounter% %% 1000" 
if !loopBatch! == 0 (echo %loopCounter% iterations done)
goto :numLoop

:incInit
  rem echo %init% mod %iter% == %func%; Increasing init
  SET iter=1 
  SET /a init+=1 
  goto :eof

:incIter
  rem echo %init% mod %iter% == %func%; Increasing iter
  SET /a iter+=1 
  goto :eof

:done
  echo. 
  ECHO Val = %init% 

关于batch-file - 在批处理脚本中执行模算术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38715127/

相关文章:

java - 在 Java 中运行批处理文件

batch-file - 如何在不隐藏控制台窗口的情况下运行批处理文件?

windows - 批处理变量被设置为 ■1 而不是预期的输出

msbuild - 如何同时使用批处理文件在多个目录中创建 MSBuild 解决方案?

command-line - 在 IF 语句中使用 "SET/P"

batch-file - 命名参数如何传递给批处理文件?

windows-7 - 没有/d为什么不能在命令行中启动程序? (Windows 7x64)

batch-file - 使用正则表达式批量搜索文件内容

batch-file - %%~dpa 的含义?

窗口/命令 |如果例如,如何断线字符数达到?