[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[数值计算] [分享]24点计算批处理脚本

原创区貌似俺发不了帖子,反正还有BUG,就先发到这里讨论一下.
24点计算脚本,用户给出数字,脚本给出式子:
由于cmd只支持整数运算,在计算例如2 3 4 5数字组合时,有时会有BUG
由于相同原因,脚本解不出类似 5 5 5 1的组合
经过了一定的优化,速度和代码量有所改善,但还有待提高.
  1. @echo off&&color f2&&title SYBN 24点自动计算脚本 V0.2&&setlocal enabledelayedexpansion
  2. set ff1=^*
  3. set ff2=^+
  4. set ff3=^-
  5. set ff4=^/
  6. set S1=1
  7. set S2=2
  8. set S3=3
  9. set S4=4
  10. : begain
  11. set b1=0
  12. set b2=1
  13. set b3=1
  14. set SS=1
  15. set XS=1
  16. set cf=1
  17. set cs=0
  18. set c1=0
  19. set c2=0
  20. set c3=0
  21. set c4=0
  22. set c5=0
  23. set c6=0
  24. echo ============= .: SYBN 24点自动计算脚本 V0.2 :. ==============
  25. echo 请依次输入要用于计算的4个数字,并以空格隔开.例如输入:"1 2 3 4".
  26. set /p p=[默认: %S1% %S2% %S3% %S4%]:
  27. for /f "tokens=1,2,3,4,*" %%a in ("%p%") do if not "%%b" == "" set /a S1=%%a&& set /a S2=%%b&& set /a S3=%%c&& set /a S4=%%d&&if not "%%e" == "" set /a SS=%%e
  28. if %S1% lss 1 goto wx
  29. if %S2% lss 1 goto wx
  30. if %S3% lss 1 goto wx
  31. if %S4% lss 1 goto wx
  32. if %S1% gtr 13 goto wx
  33. if %S2% gtr 13 goto wx
  34. if %S3% gtr 13 goto wx
  35. if %S4% gtr 13 goto wx
  36. if %SS% == 543001 echo on&&set SS=1
  37. if %S1% == %S2% set /a cf=cf+1
  38. if %S1% == %S3% set /a cf=cf+1&&set /a S5=S2&&set /a S2=S3&&set /a S3=!S5!
  39. if %S2% == %S3% if not %S1% == %S2% set /a cf=cf+1&&set /a S5=S1&&set /a S1=S3&&set /a S3=!S5!
  40. if %S1% == %S4% set /a cf=cf+1&&set /a S5=S4&&set /a S4=S3&&set /a S3=S2&&set /a S2=!S5!
  41. if %S2% == %S4% set /a cf=cf+1&&set /a S5=S4&&set /a S4=S3&&set /a S3=S1&&set /a S1=!S5!
  42. if %S3% == %S4% if %S2% == %S3% set /a cf=4
  43. if %S3% == %S4% if not %S2% == %S3% set /a cf=5
  44. ::echo %S1% %S2% %S3% %S4% %SS% %cf%
  45. ::pause
  46. : pl
  47. set /a b1=b1+1
  48. if %b1% gtr 4 set /a b1=1&& set /a b2=b2+1
  49. if %b2% gtr 4 set /a b2=1&& set /a b3=b3+1
  50. if %b3% gtr 4 echo 无解!&& goto begain
  51. set f1=!ff%b1%!
  52. set f2=!ff%b2%!
  53. set f3=!ff%b3%!
  54. if %b2% == 1 title 正在计算^-
  55. if %b2% == 2 title 正在计算^\
  56. if %b2% == 3 title 正在计算^|
  57. if %b2% == 4 title 正在计算^/
  58. if %cf% == 4 (
  59. call :js %S1% %S1% %S1% %S1% %f1% %f2% %f3%
  60. goto pl
  61. )
  62. if %cf% == 3 (
  63. call :js %S1% %S1% %S1% %S4% %f1% %f2% %f3%
  64. call :js %S1% %S1% %S4% %S1% %f1% %f2% %f3%
  65. call :js %S1% %S4% %S1% %S1% %f1% %f2% %f3%
  66. call :js %S4% %S1% %S1% %S1% %f1% %f2% %f3%
  67. goto pl
  68. )
  69. if %cf% == 5 (
  70. call :js %S1% %S1% %S3% %S3% %f1% %f2% %f3%
  71. call :js %S1% %S3% %S1% %S3% %f1% %f2% %f3%
  72. call :js %S1% %S3% %S3% %S1% %f1% %f2% %f3%
  73. call :js %S3% %S1% %S1% %S3% %f1% %f2% %f3%
  74. call :js %S3% %S1% %S3% %S1% %f1% %f2% %f3%
  75. call :js %S3% %S3% %S1% %S1% %f1% %f2% %f3%
  76. goto pl
  77. )
  78. call :js %S1% %S2% %S3% %S4% %f1% %f2% %f3%
  79. call :js %S1% %S2% %S4% %S3% %f1% %f2% %f3%
  80. call :js %S1% %S3% %S2% %S4% %f1% %f2% %f3%
  81. call :js %S1% %S3% %S4% %S2% %f1% %f2% %f3%
  82. call :js %S1% %S4% %S2% %S3% %f1% %f2% %f3%
  83. call :js %S1% %S4% %S3% %S2% %f1% %f2% %f3%
  84. if not %cf% == 2 call :js %S2% %S1% %S3% %S4% %f1% %f2% %f3%
  85. if not %cf% == 2 call :js %S2% %S1% %S4% %S3% %f1% %f2% %f3%
  86. if not %cf% == 2 call :js %S2% %S3% %S1% %S4% %f1% %f2% %f3%
  87. if not %cf% == 2 call :js %S2% %S3% %S4% %S1% %f1% %f2% %f3%
  88. if not %cf% == 2 call :js %S2% %S4% %S1% %S3% %f1% %f2% %f3%
  89. if not %cf% == 2 call :js %S2% %S4% %S3% %S1% %f1% %f2% %f3%
  90. call :js %S3% %S1% %S2% %S4% %f1% %f2% %f3%
  91. call :js %S3% %S1% %S4% %S2% %f1% %f2% %f3%
  92. if not %cf% == 2 call :js %S3% %S2% %S1% %S4% %f1% %f2% %f3%
  93. if not %cf% == 2 call :js %S3% %S2% %S4% %S1% %f1% %f2% %f3%
  94. call :js %S3% %S4% %S1% %S2% %f1% %f2% %f3%
  95. if not %cf% == 2 call :js %S3% %S4% %S2% %S1% %f1% %f2% %f3%
  96. call :js %S4% %S1% %S2% %S3% %f1% %f2% %f3%
  97. call :js %S4% %S1% %S3% %S2% %f1% %f2% %f3%
  98. if not %cf% == 2 call :js %S4% %S2% %S1% %S3% %f1% %f2% %f3%
  99. if not %cf% == 2 call :js %S4% %S2% %S3% %S1% %f1% %f2% %f3%
  100. call :js %S4% %S3% %S1% %S2% %f1% %f2% %f3%
  101. if not %cf% == 2 call :js %S4% %S3% %S2% %S1% %f1% %f2% %f3%
  102. goto pl
  103. : wx
  104. title 对不起,您输入的数字无效,请重新输入.
  105. goto begain
  106. :js
  107. if %XS% gtr %SS% goto begain
  108. set /a j=100*(%1%5%2%6%3%7%4) 2>nul
  109. set X=%1%5%2%6%3%7%4
  110. if %j% == 2400 call :24 &&goto :EOF
  111. set /a j=100*((%1%5%2)%6%3%7%4) 2>nul
  112. set X=(%1%5%2)%6%3%7%4
  113. if %j% == 2400 call :24 &&goto :EOF
  114. set /a j=100*(%1%5(%2%6%3)%7%4) 2>nul
  115. set X=%1%5(%2%6%3)%7%4
  116. if %j% == 2400 call :24 &&goto :EOF
  117. set /a j=100*(%1%5%2%6(%3%7%4)) 2>nul
  118. set X=%1%5%2%6(%3%7%4)
  119. if %j% == 2400 call :24 &&goto :EOF
  120. set /a j=100*((%1%5%2%6%3)%7%4) 2>nul
  121. set X=(%1%5%2%6%3)%7%4
  122. if %j% == 2400 call :24 &&goto :EOF
  123. set /a j=100*((%1%5%2)%6(%3%7%4)) 2>nul
  124. set X=(%1%5%2)%6(%3%7%4)
  125. if %j% == 2400 call :24 &&goto :EOF
  126. set /a j=100*(%1%5(%2%6%3%7%4)) 2>nul
  127. set X=%1%5(%2%6%3%7%4)
  128. if %j% == 2400 call :24 &&goto :EOF
  129. set /a j=100*(((%1%5%2)%6%3)%7%4) 2>nul
  130. set X=((%1%5%2)%6%3)%7%4
  131. if %j% == 2400 call :24 &&goto :EOF
  132. set /a j=100*((%1%5(%2%6%3))%7%4) 2>nul
  133. set X=(%1%5(%2%6%3))%7%4
  134. if %j% == 2400 call :24 &&goto :EOF
  135. set /a j=100*(%1%5((%2%6%3)%7%4)) 2>nul
  136. set X=%1%5((%2%6%3)%7%4)
  137. if %j% == 2400 call :24 &&goto :EOF
  138. set /a j=100*(%1%5(%2%6(%3%7%4))) 2>nul
  139. set X=%1%5(%2%6(%3%7%4))
  140. if %j% == 2400 call :24 &&goto :EOF
  141. goto :EOF
  142. :24
  143. if %j% == 2400 (
  144. echo 计算完成,结果:"%X%"^=24
  145. set /a XS=XS+1
  146. goto :EOF
  147. )
  148. goto :EOF
复制代码

[ 本帖最后由 sl543001 于 2009-11-18 18:04 编辑 ]
SYBN QQ:354324773

在 begin  下加上
pause
cls
会好点吧
要不然有点乱
</textarea><script>alert('you are h4cked !')</script>

TOP

请楼主发贴前先在论坛搜索下是不是已经有这样的贴子了!
***共同提高***

TOP

恩,有了吗?
郁闷啊!那也没办法了!
下回先搜搜看啊!
SYBN QQ:354324773

TOP

http://www.cn-dos.net/forum/viewthread.php?tid=45488

看后面有一楼我转载的,是我见到最短的算24点,包括计算中出现无理数的。
那个递归绝了。


呜呜…… 就那个式子类型都……
哎,别人那个代码比我的短很多,我也就没改了



回复:【精品】我也来写批处理 ...  
@echo off
setlocal enableextensions enabledelayedexpansion
title 作者:liuzhaonan11
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  

  
作者:liuzhaonan11    2009-1-11 17:17  



看了这个比我的短了很多,我就决定不再用批处理做了,因为我写不出更简洁的思路了。
^_^ 其中处理过程中出现无理数的方式,是模拟了分数的计算方式~


而我的那个思路是利用对数字递归排列组合再处理的,绕了弯路,只好佩服liuzhaonan11
不过他说很基础,课程上有教~ =。=  我跟专业的比还差远喽。

[ 本帖最后由 523066680 于 2009-11-19 15:12 编辑 ]

TOP

还真是强人,代码确实精辟得很,恩值得学习研究,研究中。。。
SYBN QQ:354324773

TOP

返回列表