很久以前网上下载的批处理算24点的脚本,忘记出处了。- @echo off
- setlocal enableextensions enabledelayedexpansion
- title 批处理算24点
- echo 批处理算24点&echo.
- :loop
- set /p "n=输入四个数用空格分开: "
- set /a "lzn=0"
- for %%i in (!n!) do set /a "lzn+=1"&set /a "lz!lzn!z=%%i,lz!lzn!m=1,lz!lzn!s=%%i"
- call:liuzhaonan11 lz
- if not errorlevel 1 echo 结果: 没有答案
- goto loop
- 原理说明:使用有理数进行计算,选择算式中最先结合的两数和它们之间的运算,并递归.
- :liuzhaonan11
- setlocal
- set "f1=+"&set "f2=-"&set "f3=*"&set "f4=/"
- for /l %%i in (1,1,!%1n!) do for /l %%j in (1,1,!%1n!) do if %%i neq %%j (
- for /l %%k in (1,1,4) do (
- setlocal
- if %%k leq 2 set /a "z=%1%%jz*%1%%im!f%%k!%1%%jm*%1%%iz,m=%1%%jm*%1%%im"
- if %%k equ 3 set /a "z=%1%%jz*%1%%iz,m=%1%%jm*%1%%im"
- if %%k equ 4 set /a "z=%1%%jz*%1%%im,m=%1%%jm*%1%%iz"
- if !%1n! equ 2 (
- set /a "t=!z!-24*!m!"
- if !t! equ 0 if !m! neq 0 echo 结果: !%1%%js! !f%%k! !%1%%is! = 24&exit /b 1
- ) else (
- set "s=(!%1%%js! !f%%k! !%1%%is!)"&set /a "n=0"
- for /l %%l in (1,1,!%1n!) do if %%l neq %%i if %%l neq %%j (
- set /a "n+=1"&set "%1!n!z=!%1%%lz!"&set "%1!n!m=!%1%%lm!"&set "%1!n!s=!%1%%ls!"
- )
- set /a "n+=1"&set "%1!n!z=!z!"&set "%1!n!m=!m!"&set "%1!n!s=!s!"
- set "%1n=!n!"&call%0 %1
- if errorlevel 1 exit /b 1
- )
- endlocal
- )
- )
- exit /b 0
复制代码
|