Board logo

标题: [其他] 批处理及多种语言实现 CWHGN猜数字 [打印本页]

作者: happy886rr    时间: 2017-8-9 22:00     标题: 批处理及多种语言实现 CWHGN猜数字

CWHGN猜数字具体要求请参见帖子   http://www.bathome.net/thread-44826-1-2.html

我先用看似单纯的批处理去实现CWHGN猜数字游戏,虽然批处理不支持发送请求,但凭借调用一个4行的外部js可以实现网络请求功能。索性集成到一个批文。由于该题目涉及到数组的频繁操作,每次post批处理需要先筛选几千次,因此 处理较大的数组 就暴露了其运行缓慢的缺陷:每分钟猜题速度10左右。若改用位运算可以缓解一些,但提速不大。测试效果和代码如下:
(图片引用均为外部链接)

[批处理实现]略带js混编
  1. 1>1/* :
  2. @echo off
  3. REM 开启变量延迟
  4. setlocal enabledelayedexpansion
  5. REM 设置全局变量
  6. set "USER_NAME=adad"
  7. set "PASS_WORD=123"
  8. set "POST_URL=http://www.codetiger.win/extra/API.php"
  9. REM 生成[0-9]的四位全排列数
  10. set "PM_LENGTH=0"
  11. for /l %%i in (0,1,9) do (
  12. for /l %%j in (0,1,9) do (
  13. for /l %%k in (0,1,9) do (
  14. for /l %%l in (0,1,9) do (
  15. 2> NUL  set/a "1/((%%i-%%j)*(%%i-%%k)*(%%i-%%l)*(%%j-%%k)*(%%j-%%l)*(%%k-%%l))" && (
  16. set/a "PM_LENGTH+=1"
  17. set "PM[!PM_LENGTH!]=%%i%%j%%k%%l"
  18. )
  19. )
  20. )
  21. )
  22. )
  23. REM 初始化标记数组
  24. for /l %%i in (1,1,!PM_LENGTH!) do (
  25. set "PM_MARK[%%i]=1"
  26. )
  27. REM 初始化变量值
  28. set/a "RESP_TOKENS=0, RESP_DATA=0, GUESS_TIMES=0, CUR_INDEX=1"
  29. REM 锚点
  30. :LOOP1
  31. REM 利用局部变量表,实现快速初始化
  32. setlocal
  33. REM 内层循环
  34. :LOOP2
  35. REM 变量中转
  36. for %%a in (!CUR_INDEX!) do (
  37. set "GUESS_NUMBER=!PM[%%a]!"
  38. )
  39. REM 向服务器发送请求,并处理返回json格式
  40. for /f "tokens=2,4,6,8,10 delims=,:{}" %%a in ('cscript -nologo -e:jscript "%~f0" "%POST_URL%" "%USER_NAME%" "%PASS_WORD%" !GUESS_NUMBER!') do (
  41. REM 猜数计次器
  42. set/a "GUESS_TIMES+=1"
  43. REM 显示界面
  44. set/a "DIS_CUR_INDEX=10000+CUR_INDEX"
  45. echo #POST:{PM[!DIS_CUR_INDEX:~-4!]=!GUESS_NUMBER!}  #RESP_JSON:{%%a %%b %%c %%d %%e}
  46. REM 网络检测器
  47. if "%%e"=="" (
  48. echo Requests error^^!
  49. endlocal
  50. goto :LOOP1
  51. )
  52. REM 答案验证器
  53. if "%%b"=="4" (
  54. echo Takes:[!GUESS_TIMES!th], Answer:!GUESS_NUMBER!, Score:[%%d]
  55. endlocal
  56. goto :LOOP1
  57. )
  58. REM TOKENS重置器
  59. if "!RESP_TOKENS!"=="0" (
  60. set "RESP_TOKENS=%%e"
  61. ) else (
  62. if not "!RESP_TOKENS!"=="%%e" (
  63. echo Reset...
  64. endlocal
  65. goto :LOOP1
  66. )
  67. )
  68. REM 计算猜测权值
  69. set/a "R_WEIGHTS=%%b*5+%%c, NEW_INDEX=0"
  70. REM 权值筛选器
  71. for /l %%i in (1,1,!PM_LENGTH!) do (
  72. if !PM_MARK[%%i]! equ 1 (
  73. set "T_WEIGHTS=0"
  74. REM 数码比对器
  75. for /l %%p in (0,1,3) do (
  76. for /f "tokens=1,2" %%A in ("!PM[%%i]:~%%p,1! !CUR_INDEX!") do (
  77. if not "!PM[%%B]:%%A=!"=="!PM[%%B]!" (
  78. if "!PM[%%B]:~%%p,1!"=="%%A" (
  79. set/a "T_WEIGHTS+=5"
  80. ) else (
  81. set/a "T_WEIGHTS+=1"
  82. )
  83. )
  84. )
  85. )
  86. REM 筛选标记器
  87. if !T_WEIGHTS! neq !R_WEIGHTS! (
  88. set "PM_MARK[%%i]=0"
  89. ) else (
  90. if !CUR_INDEX! neq %%i (
  91. set "NEW_INDEX=%%i"
  92. )
  93. )
  94. )
  95. )
  96. REM 索引价值判定
  97. if !NEW_INDEX! neq 0 (
  98. set "CUR_INDEX=!NEW_INDEX!"
  99. goto :LOOP2
  100. ) else (
  101. echo Fault.
  102. endlocal
  103. goto :LOOP1
  104. )
  105. )
  106. REM 内层循环结尾
  107. goto :LOOP2
  108. */
  109. var xmlHTTP=new ActiveXObject('Msxml2.XMLHTTP');
  110. xmlHTTP.open('SEND', WScript.arguments(0) + '?username=' + WScript.arguments(1) + '&password=' + WScript.arguments(2) + '&send=answer&number=' + WScript.arguments(3), false);
  111. xmlHTTP.send();
  112. WScript.echo(xmlHTTP.responseText);
复制代码
.
.
之后我改用了java语言,采用正则表达式去解析json。由于java是字节码,运行速度已经发送了质的改变。将它编译成了跨平台jar包后,可以在多种设备上运行。在手机上跑,无需jre,以微乎其微的耗电量和ARM芯片占用率刷榜单。 请下载附件后 (把zip后缀改成jar即可运行):链接: https://pan.baidu.com/s/1qiEzR32fxyorm9SZGBUXVQ?pwd=345e
调用参数cwhgn.jar -u [用户名] -p [用户密码], 当用户名未注册时,程序会自动帮你注册。手机端建议使用ej 脚本运行。

[java实现]Main.java
  1. package com.android.test;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9. /**
  10. * @author Happy from Bathome
  11. * @time
  12. */
  13. public class Main
  14. {
  15. private static final String REGU_URL = "http://www.codetiger.win/extra/index.php";
  16. private static final String POST_URL = "http://www.codetiger.win/extra/API.php";
  17. private static final int MAX_ARRAY_SIZE = 5040;
  18. private static boolean[] markPmArray = new boolean[MAX_ARRAY_SIZE];
  19. private static final Pattern reCode = Pattern.compile("\\{\\\"code\\\":(\\d+)");
  20. private static final Pattern re = Pattern.compile("\\{\\\"code\\\":(\\d+),\\\"A\\\":(\\d+),\\\"B\\\":(\\d+),\\\"count\\\":(\\d+),\\\"tokens\\\":\\\"(\\d+)\\\"\\}");
  21. private static ResponseJson responseJson = new ResponseJson();
  22. public static void main(String[] args)
  23. {
  24. if(args.length !=4 || !args[0].equals("-u") || !args[2].equals("-p"))
  25. {
  26. System.out.println("Usage: java -jar cwhgn.jar -u [userName] -p [passWord]");
  27. System.exit(1);
  28. }
  29. String userName = args[1];
  30. String userPassword = args[3];
  31. String responseString = sendPost(POST_URL, "send=answer&number=0123&username=" + userName + "&password=" + userPassword).toString();
  32. Matcher m = reCode.matcher(responseString);
  33. if(m.find())
  34. {
  35. if(Integer.parseInt(m.group(1)) == 300)
  36. {
  37. int regRet = regUser(userName, userPassword);
  38. if(regRet != 100)
  39. {
  40. System.out.printf("The userName '%s' has been registered or the userPassword '%s' is wrong!\n", userName, userPassword);
  41. System.exit(1);
  42. }
  43. }
  44. }
  45. else
  46. {
  47. System.out.println("Please try again!");
  48. System.exit(1);
  49. }
  50. String[] pmArray = getPermutations();
  51. int pmArrayLen = pmArray.length;
  52. long startMilis=System.currentTimeMillis();
  53. long guessScore=0;
  54. long guessSpeed=0;
  55. while (true)
  56. {
  57. int currentIndex = 0;
  58. long responseTokens = -1;
  59. int guessTimes = 0;
  60. for(int i=0; i<MAX_ARRAY_SIZE; i++)
  61. {
  62. markPmArray[i] = true;
  63. }
  64. while (true)
  65. {
  66. try
  67. {
  68. responseString = sendPost(POST_URL, "send=answer&username=" + userName + "&password=" + userPassword + "&number="+ pmArray[currentIndex]).toString();
  69. m = re.matcher(responseString);
  70. if(! m.find())
  71. {
  72. System.out.println("Received data failed!");
  73. continue;
  74. //break;
  75. }
  76. responseJson.getJson(Integer.parseInt(m.group(1),10), Integer.parseInt(m.group(2),10), Integer.parseInt(m.group(3),10), Integer.parseInt(m.group(4),10), Long.parseLong(m.group(5)));
  77. }
  78. catch (Exception e)
  79. {
  80. continue;
  81. //break;
  82. }
  83. if(guessScore == 0)
  84. {
  85. guessScore = responseJson.count;
  86. }
  87. guessTimes ++;
  88. if(responseJson.A == 4)
  89. {
  90. if(System.currentTimeMillis()-startMilis > 6000)
  91. {
  92. guessSpeed = (responseJson.count - guessScore);
  93. guessScore = responseJson.count;
  94. startMilis = System.currentTimeMillis();
  95. }
  96. System.out.printf("Takes:[%d cycles], Answer:%s, Score:[%d +%d/min]\n", guessTimes, pmArray[currentIndex], responseJson.count, guessSpeed*10);
  97. break;
  98. }
  99. if(responseTokens == -1)
  100. {
  101. responseTokens = responseJson.tokens;
  102. }
  103. else if(responseTokens != responseJson.tokens)
  104. {
  105. System.out.println("Reset ...");
  106. break;
  107. }
  108. currentIndex = guessCore(pmArray, pmArrayLen, responseJson.A*5+responseJson.B, pmArray[currentIndex]);
  109. }
  110. }
  111. }
  112. private static int regUser(String userName, String userPassword)
  113. {
  114. String responseString = sendPost(REGU_URL, "send=reg&qq=123456789&username=" + userName + "&password="+ userPassword).toString();
  115. Matcher m = reCode.matcher(responseString);
  116. return (m.find()) ?(Integer.parseInt(m.group(1), 10)) :0;
  117. }
  118. private static int getWeights(String correctNum, String guessNum)
  119. {
  120. int retWeights = 0;
  121. for(int i=0; i<4; i++)
  122. {
  123. int digitalPosition = guessNum.indexOf((correctNum.substring(i, i+1)));
  124. if(digitalPosition != -1)
  125. {
  126. retWeights += (digitalPosition==i)?5:1;
  127. }
  128. }
  129. return retWeights;
  130. }
  131. private static int guessCore(String[] inputPmArray, int inputPmArrayLen, int responseWeights, String guessNum)
  132. {
  133. int markIndex = 0;
  134. for(int i=0; i<inputPmArrayLen; i++)
  135. {
  136. if(markPmArray[i])
  137. {
  138. if(getWeights(inputPmArray[i], guessNum) != responseWeights)
  139. {
  140. markPmArray[i] = false;
  141. }
  142. else if(markIndex == 0)
  143. {
  144. markIndex = i;
  145. }
  146. }
  147. }
  148. return markIndex;
  149. }
  150. private static String[] getPermutations()
  151. {
  152. String[] pmArray = new String[MAX_ARRAY_SIZE];
  153. int index=0;
  154. for(int i=0; i<10; i++)
  155. for(int j=0; j<10; j++)
  156. for(int k=0; k<10; k++)
  157. for(int l=0; l<10; l++)
  158. {
  159. if((i-j)*(i-k)*(i-l)*(j-k)*(j-l)*(k-l)!=0)
  160. {
  161. pmArray[index++]= "" + i + j + k + l;
  162. }
  163. }
  164. return pmArray;
  165. }
  166. public static StringBuffer sendPost(String postURL, String postData)
  167. {
  168. PrintWriter pWriter = null;
  169. BufferedReader bReader = null;
  170. StringBuffer sBuffer = new StringBuffer("");
  171. try
  172. {
  173. URL realUrl = new URL(postURL);
  174. URLConnection conn = realUrl.openConnection();
  175. conn.setRequestProperty("accept", "*/*");
  176. conn.setRequestProperty("connection", "keep-alive");
  177. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  178. conn.setDoOutput(true);
  179. conn.setDoInput(true);
  180. pWriter = new PrintWriter(conn.getOutputStream());
  181. pWriter.print(postData);
  182. pWriter.flush();
  183. bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  184. String line = null;
  185. while ((line = bReader.readLine()) != null)
  186. {
  187. sBuffer.append(line);
  188. }
  189. }
  190. catch (Exception e)
  191. {
  192. System.out.println("An exception occurred while sending a POST request!"+e);
  193. }
  194. finally
  195. {
  196. try
  197. {
  198. pWriter.close();
  199. bReader.close();
  200. }
  201. catch (Exception e)
  202. {
  203. // TODO: handle exception
  204. }
  205. }
  206. return sBuffer;
  207. }
  208. protected static class ResponseJson
  209. {
  210. int code, A, B, count;
  211. long tokens;
  212. void getJson(int inputCode, int inputA, int inputB, int inputCount, long inputTokens)
  213. {
  214. this.code = inputCode;
  215. this.A = inputA;
  216. this.B = inputB;
  217. this.count = inputCount;
  218. this.tokens = inputTokens;
  219. }
  220. }
  221. }
复制代码
.
.
用其他语言的实现,思路基本相同,就当复习语法。
>>
[vbs版]
  1. '&@echo off&cls&cscript -nologo -e:vbscript "%~f0" %*&pause&exit /b
  2. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  3. Dim markPM(5040)
  4. GuessNumber "adad", "123", "http://www.codetiger.win/extra/API.php"
  5. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  6. Function GetPermutations()
  7. Dim retArray(5040), index
  8. index = 0
  9. For i = 0 To 9
  10. For j = 0 To 9
  11. For k = 0 To 9
  12. For l = 0 To 9
  13. If ((i-j)*(i-k)*(i-l)*(j-k)*(j-l)*(k-l)) <> 0 Then
  14. retArray(index) = i & j & k & l & ""
  15. index = index + 1
  16. End If
  17. Next
  18. Next
  19. Next
  20. Next
  21. GetPermutations = retArray
  22. End Function
  23. Function GetWeights(correctNum, guessNum)
  24. Dim retWeights
  25. retWeights = 0
  26. For i = 1 To 4
  27. For j = 1 To 4
  28. If Mid(correctNum, i, 1) = Mid(guessNum, j, 1) Then
  29. If i = j Then
  30. retWeights = retWeights + 5
  31. Else
  32. retWeights = retWeights + 1
  33. End If
  34. End If
  35. Next
  36. Next
  37. GetWeights = retWeights
  38. End Function
  39. Function GuessCore(arrayPM, arrayPMLen, responseWeights, guessIndex)
  40. Dim markIndex
  41. markIndex = -1
  42. For i = 0 To arrayPMLen-1
  43. If markPM(i) Then
  44. If GetWeights(arrayPM(i), arrayPM(guessIndex)) <> responseWeights Then
  45. markPM(i) = False
  46. ElseIf  i <> guessIndex And markIndex = -1 Then
  47. markIndex = i
  48. End If
  49. End If
  50. Next
  51. GuessCore = markIndex
  52. End Function
  53. Function HttpPostData(hostURL, postString)
  54. Dim respJson
  55. With CreateObject("MSXML2.XMLHTTP")
  56. .Open "POST", hostURL, False
  57. .setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
  58. .setRequestHeader "CONNECTION", "Keep-Alive"
  59. .send postString
  60. respJson = .responseText
  61. End With
  62. HttpPostData = Split(replace(respJson, ":", ","), ",")
  63. End Function
  64. Function GuessNumber(userName, passWord, hostURL)
  65. Dim pmArray, pmArrayLen
  66. Dim startMilis, guessScore, guessSpeed
  67. startTime = Timer : guessScore = 0 : guessSpeed = 0
  68. pmArray = GetPermutations()
  69. pmArrayLen = Ubound(pmArray)
  70. Do
  71. Dim nIndex, responseTokens, respData, guessTimes
  72. responseString="" : nIndex=0 : responseTokens="" : espData="": guessTimes=0
  73. For i = 0 To pmArrayLen-1
  74. markPM(i) = True
  75. Next
  76. Do
  77. respData = HttpPostData(hostURL, "username=" & userName & "&password=" & passWord & "&send=answer&number=" & pmArray(nIndex))
  78. If Ubound(respData) + 1 <> 10 Then
  79. WScript.echo "Requests error!!!'"
  80. Exit Do
  81. End If
  82. If guessScore = 0 Then
  83. guessScore = respData(7)
  84. End If
  85. guessTimes = guessTimes + 1
  86. If respData(3) = "4" Then
  87. If Timer - startTime > 6 Then
  88. guessSpeed = (CLng(respData(7)) - guessScore)*10
  89. guessScore = CLng(respData(7))
  90. startTime  = Timer
  91. End If
  92. WScript.echo "Takes:[" & guessTimes & "th], Answer:" & pmArray(nIndex) & ", Score:[" & respData(7) & "  +" & guessSpeed & "/min]"
  93. Exit Do
  94. End If
  95. If responseTokens = "" Then
  96. responseTokens = respData(9)
  97. ElseIf responseTokens <> respData(9) Then
  98. WScript.echo "Reset..."
  99. Exit Do
  100. End If
  101. nIndex = GuessCore(pmArray, pmArrayLen, CInt(respData(3))*5+CInt(respData(5)), nIndex)
  102. If nIndex = -1 Then
  103. WScript.echo "Fault..."
  104. Exit Do
  105. End If
  106. Loop
  107. Loop
  108. End Function
复制代码
>>
[js混编]效率还不错
  1. 1>1/* :
  2. @echo off
  3. cscript -nologo -e:jscript "%~f0" %*
  4. pause&exit /b
  5. */
  6. /********************** MAIN PROCESS ***********************/
  7. var USER_NAME='adad';
  8. var PASS_WORD='123';
  9. var POST_URL ='http://www.codetiger.win/extra/API.php';
  10. var markPM=[];
  11. guessNumber(USER_NAME, PASS_WORD, POST_URL);
  12. /***********************************************************/
  13. function getPermutations()
  14. {
  15. var retArray=[];
  16. for(var i=0; i<10; i++)
  17. for(var j=0; j<10; j++)
  18. for(var k=0; k<10; k++)
  19. for(var l=0; l<10; l++)
  20. if((i-j)*(i-k)*(i-l)*(j-k)*(j-l)*(k-l))
  21. retArray.push('' + i + j + k + l);
  22. return retArray;
  23. }
  24. function getWeights(correctNum, guessNum)
  25. {
  26. var retWeights = 0;
  27. var gArray=guessNum.split('');
  28. for(var i=0; i<4; i++)
  29. {
  30. var dIndex=correctNum.indexOf( gArray[i]);
  31. if(dIndex != -1)
  32. {
  33. retWeights += (dIndex==i)?5:1;
  34. }
  35. }
  36. return retWeights;
  37. }
  38. function guessCore(arrayPM, arrayPMLen, responseWeights, guessNum)
  39. {
  40. var markIndex = -1;
  41. for(var i=1; i<arrayPMLen; i++)
  42. {
  43. if(markPM[i])
  44. {
  45. if(getWeights(arrayPM[i], guessNum) != responseWeights)
  46. {
  47. markPM[i] = false;
  48. }
  49. else if(markIndex == -1)
  50. {
  51. markIndex = i;
  52. }
  53. }
  54. }
  55. return markIndex;
  56. }
  57. function httpPostData(hostURL, postString)
  58. {
  59. var xmlHTTP=new ActiveXObject('Msxml2.XMLHTTP');
  60. xmlHTTP.open('POST', hostURL, false);
  61. xmlHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  62. xmlHTTP.setRequestHeader('Connection', 'Keep-Alive');
  63. xmlHTTP.send(postString);
  64. if(xmlHTTP.readyState === 4)
  65. {
  66. if(
  67.     (
  68.         (xmlHTTP.status >= 200) &&
  69.         (xmlHTTP.status <  300)
  70.     ) ||
  71.     (
  72.         (xmlHTTP.status == 304)
  73.     )
  74. )
  75. {
  76. return eval( '(' + xmlHTTP.responseText + ')' );
  77. }
  78. else
  79. {
  80. return null
  81. }
  82. }
  83. }
  84. function guessNumber(userName, passWord, hostURL)
  85. {
  86. var pmArray=getPermutations();
  87. var pmArrayLen=pmArray.length;
  88. var startMilis=new Date(), guessScore=0, guessSpeed=0;
  89. for(;;)
  90. {
  91. var currentIndex=0, responseTokens=null, respData=null, guessTimes=0;
  92. for(var i=0; i<pmArrayLen; i++)
  93. {
  94. markPM[i] = true;
  95. }
  96. for(;;)
  97. {
  98. try
  99. {
  100. respData = httpPostData(hostURL, 'username=' + userName + '&password=' + passWord + '&send=answer&number=' + pmArray[currentIndex]);
  101. if(respData == null)
  102. {
  103. continue;
  104. }
  105. }
  106. catch(e)
  107. {
  108. WScript.echo('Requests error!!!');
  109. break;
  110. }
  111. if(guessScore == 0)
  112. {
  113. guessScore = respData['count'];
  114. }
  115. guessTimes ++;
  116. if(respData['A'] == 4)
  117. {
  118. if(new Date()-startMilis > 6000)
  119. {
  120. guessSpeed=(respData['count'] - guessScore)*10;
  121. guessScore=respData['count'];
  122. startMilis=new Date();
  123. }
  124. WScript.echo('Takes:[' + guessTimes + 'th], Answer:' + pmArray[currentIndex] + ', Score:[' + respData['count'] + ' +' + guessSpeed + '/min]');
  125. break;
  126. }
  127. if(responseTokens == null)
  128. {
  129. responseTokens = respData['tokens'];
  130. }
  131. else if(responseTokens != respData['tokens'])
  132. {
  133. WScript.echo('Reset...');
  134. break;
  135. }
  136. if( (currentIndex=guessCore(pmArray, pmArrayLen, respData['A']*5+respData['B'], pmArray[currentIndex])) == -1)
  137. {
  138. WScript.echo('Fault...');
  139. break;
  140. }
  141. }
  142. }
  143. }
复制代码
>>
[python版] py3标准
  1. # -*- coding:utf-8 -*-
  2. import requests
  3. import itertools
  4. # 用户信息
  5. userNAMZE='adad'
  6. passWORD='123'
  7. # 主机地址
  8. hostURL='http://www.codetiger.win/extra/API.php'
  9.          
  10. # 筛法标记数组
  11. markPM=[]
  12. # 获取权重
  13. def GetWeights(correctNum, guessNum):
  14. retWeights=0
  15. for i in range(0,4):
  16. for j in range(0,4):
  17. if correctNum[i] == guessNum[j] :
  18. if i==j :
  19. retWeights += 5
  20. else:
  21. retWeights += 1
  22. return retWeights
  23. # 猜数筛法
  24. def GuessCore(arrayPM, arrayLen, responseWeights, guessNum):
  25. markIndex=None
  26. for i in range(0, arrayPMLen):
  27. if markPM[i]:
  28. if GetWeights(arrayPM[i], guessNum) != responseWeights :
  29. markPM[i]=False
  30. elif markIndex == None :
  31. markIndex=i
  32. if markIndex==None :
  33. markIndex=0
  34. return markIndex
  35. # 主函数
  36. if __name__ == "__main__":
  37. # 生成由[0-9]构成的4位全排列数
  38. arrayPM=[]
  39. for r in itertools.permutations('0123456789', 4):
  40. arrayPM.append(''.join(r))
  41. # 获取全排列数组长度
  42. arrayPMLen = len(arrayPM)
  43. # 根据全排列数组长度,分配标记数组
  44. markPM=[False]*arrayPMLen
  45. # 外层循环主程
  46. while True:
  47. # 标记数组重置为真
  48. for i in range(0, arrayPMLen):
  49. markPM[i]=True
  50. # 重置数组索引
  51. currentIndex=0
  52. # 重置猜测次数
  53. guessTimes=0
  54. # 重置tokens
  55. responseTokens=None
  56. #内层循环猜测
  57. while True:
  58. # 提交数字,返回json格式
  59. try:
  60. r = requests.post(hostURL, {'password':passWORD, 'username':userNAMZE, 'number':arrayPM[currentIndex], 'send':'answer'}).json()
  61. except:
  62. print("Requests error!!!")
  63. break
  64. # 猜测次数自增
  65. guessTimes += 1
  66. # 判断是否猜测正确
  67. if r['A'] == 4 :
  68. print("Takes {0} times: {1}".format(guessTimes, arrayPM[currentIndex]))
  69. break
  70. # 如果tokens为空更新tokens
  71. if responseTokens == None:
  72. responseTokens = r['tokens']
  73. # 如果tokens改则跳出内层循环
  74. elif responseTokens != r['tokens'] :
  75. print("Reset...")
  76. break
  77. # 调用猜数字筛法函数
  78. currentIndex = GuessCore(arrayPM, arrayPMLen, r['A']*5+r['B'], arrayPM[currentIndex])
复制代码
>>
[C语言版]cfree编译
  1. #include    <stdio.h>
  2. #include  <stdbool.h>
  3. #include   <string.h>
  4. #include  <windows.h>
  5. #include <winsock2.h>
  6. #include     <time.h>
  7. //定义用户信息
  8. #define USER_NAME    "adad"
  9. #define PASS_WORD    "123"
  10. //=======================================================//
  11. #define POST_HEAD "\
  12. POST /extra/API.php HTTP/1.1\r\n\
  13. TE: deflate,gzip;q=0.3\r\n\
  14. Connection: Keep-Alive\r\n\
  15. Host: www.codetiger.win\r\n\
  16. Content-Length: 50\r\n\
  17. Content-Type: application/x-www-form-urlencoded\r\n\
  18. \r\n"
  19. //=======================================================//
  20. //宏定义数组阈值
  21. #define MAX_POST_SIZE  512
  22. #define MAX_ARRAY_SIZE 5040
  23. //定义SCOKET收发阈值
  24. #define RECEIVE_SIZE   512
  25. #define SEND_SIZE      512
  26. //POST 发送容器
  27. static char sendBuf[MAX_POST_SIZE] = POST_HEAD  "username="USER_NAME  "&password="PASS_WORD  "&send=answer&number=";
  28. //Json number开始索引
  29. static int sendbuf_JSON_NUMBER_OFFSET;
  30. //数组pmArray
  31. static char pmArray[MAX_ARRAY_SIZE][4];
  32. //筛法标记数组
  33. static bool markPM[MAX_ARRAY_SIZE]= {false};
  34. //返回Json结构体
  35. typedef struct{
  36. int A;
  37. int B;
  38. int count;
  39. long long tokens;
  40. } RESPJSON, *PRESPJSON;
  41. //ParseReceiveBuf Json解析函数
  42. int ParseReceiveBuf(PRESPJSON pRespJson, const char* receiveBuf)
  43. {
  44. int ret = 0;
  45. char* p = NULL;
  46. ((p=strstr(receiveBuf, "\"A\":")) != NULL)? (pRespJson->A = atoi(p+4)) :(ret=1);
  47. ((p=strstr(receiveBuf, "\"B\":")) != NULL)? (pRespJson->B = atoi(p+4)) :(ret=1);
  48. ((p=strstr(receiveBuf, "\"count\":")) != NULL)? (pRespJson->count = atoi(p+8)) :(ret=1);
  49. ((p=strstr(receiveBuf, "\"tokens\":")) != NULL)? (pRespJson->tokens = atoll(p+10)) :(ret=1);
  50. return ret;
  51. }
  52. //初始化数组pmArray,生成4位全排列
  53. int InitpmArray()
  54. {
  55. int i, j, k, l, index=0;
  56. for(i=0; i<10; i++)
  57. for(j=0; j<10; j++)
  58. for(k=0; k<10; k++)
  59. for(l=0; l<10; l++)
  60. {
  61. if((i-j)*(i-k)*(i-l)*(j-k)*(j-l)*(k-l))
  62. {
  63. pmArray[index][0] = i+'0';
  64. pmArray[index][1] = j+'0';
  65. pmArray[index][2] = k+'0';
  66. pmArray[index][3] = l+'0';
  67. index ++;
  68. }
  69. }
  70. return 0;
  71. }
  72. //获取权重
  73. int GetWeights(char* correctNum, char* guessNum)
  74. {
  75. int i, j, retWeights = 0;
  76. for(i=0; i<4; i++)
  77. for(j=0; j<4; j++)
  78. {
  79. if (correctNum[i] == guessNum[j])
  80. {
  81. retWeights += (i==j)?5:1;
  82. }
  83. }
  84. return retWeights;
  85. }
  86. //猜数筛法
  87. int GuessCore(int responseWeights, int currentIndex)
  88. {
  89. int i, markIndex = -1;
  90. for(i=0; i<MAX_ARRAY_SIZE; i++)
  91. {
  92. if(markPM[i])
  93. {
  94. if(GetWeights(pmArray[i], pmArray[currentIndex]) != responseWeights)
  95. {
  96. markPM[i] = false;
  97. }
  98. else if( (markIndex <i ) && (i != currentIndex) )
  99. {
  100. markIndex = i;
  101. }
  102. }
  103. }
  104. return markIndex;
  105. }
  106. //猜数字核心函数
  107. int GuessNumber(struct sockaddr_in sAddr)
  108. {
  109. //初始化数组pmArray,生成4位全排列
  110. InitpmArray();
  111. //声明参数、计数器
  112. long long responseTokens;
  113. int currentIndex, guessTimes;
  114. //POST返回结构接收容器
  115. char receiveBuf[RECEIVE_SIZE];
  116. //Json返回结构体
  117. RESPJSON respJson={0}, *pRespJson = &respJson;
  118. //声明安全套接口
  119. SOCKET uintScoket;
  120. for(;;)
  121. {
  122. //重置布尔标记数组
  123. memset(markPM, true, MAX_ARRAY_SIZE);
  124. //重置各类参数、计数器
  125. currentIndex =2519/*rand()%MAX_ARRAY_SIZE*/, guessTimes =0, responseTokens =-1;
  126. for(;;)
  127. {
  128. //装入猜测数字 pmArray[currentIndex]
  129. strncpy(sendBuf+sendbuf_JSON_NUMBER_OFFSET, pmArray[currentIndex], 4);
  130. //生成一个安全套接口
  131. if((uintScoket = socket(AF_INET,SOCK_STREAM,0)) < 0)
  132. {
  133. fprintf(stderr, "Socket error!\n");
  134. break;
  135. }
  136. //建立安全套接口连接
  137. if(connect(uintScoket,(const struct sockaddr*)&sAddr,sizeof(sAddr)) < 0)
  138. {
  139. fprintf(stderr, "Connet error!\n");
  140. break;
  141. }
  142. //POST发送
  143. if(send(uintScoket,sendBuf,SEND_SIZE,0) < 0)
  144. {
  145. fprintf(stdout, "Send error!!!\n");
  146. break;
  147. }
  148. //POST接收
  149. int respINT = -1;
  150. if((respINT = recv(uintScoket,receiveBuf,RECEIVE_SIZE,0)) < 0)
  151. {
  152. fprintf(stdout, "Receive error!!!\n");
  153. break;
  154. }
  155. //关闭安全套接口
  156. closesocket(uintScoket);
  157. //解析POST返还信息
  158. if(ParseReceiveBuf(pRespJson,receiveBuf) == 1){
  159. fprintf(stdout, "Parse json failed!\n");
  160. break;
  161. }
  162. //猜数计数器
  163. guessTimes ++;
  164. //猜数正确,跳出内层循环
  165. if(pRespJson->A == 4)
  166. {
  167. fprintf(stdout, "=======Takes:%dth, Number:%-4.4s, Score:%d\n", guessTimes, pmArray[currentIndex], pRespJson->count);
  168. break;
  169. }
  170. //判断tokens重置
  171. if(responseTokens == -1)
  172. {
  173. responseTokens = pRespJson->tokens;
  174. }
  175. else if(responseTokens != pRespJson->tokens)
  176. {
  177. fprintf(stdout, "Reset...\n");
  178. break;
  179. }
  180. //筛选标记数组
  181. if((currentIndex=GuessCore(pRespJson->A*5+pRespJson->B, currentIndex)) == -1)
  182. {
  183. fprintf(stdout, "Error!!!\n");
  184. break;
  185. }
  186. }
  187. }
  188. return 0;
  189. }
  190. //------ MAIN 主函数 ------
  191. int main()
  192. {
  193. //初始化随机种子
  194. srand((unsigned)time(NULL));
  195. //获取Json number 开始索引
  196. sendbuf_JSON_NUMBER_OFFSET = strlen(sendBuf);
  197. //启动安全套接字
  198. WSADATA wsData;
  199. if(WSAStartup(MAKEWORD(2, 1), &wsData) !=0)
  200. {
  201. fprintf(stderr, "Starting the winsocket failed\n");
  202. exit(1);
  203. }
  204. //填充地址结构
  205. struct sockaddr_in sAddr={0};
  206. sAddr.sin_family = AF_INET;
  207. sAddr.sin_port = htons(80);
  208. sAddr.sin_addr.S_un.S_addr = inet_addr("43.240.12.145");
  209. //调用猜数字核心函数
  210. GuessNumber(sAddr);
  211. //关闭安全套接字
  212. WSACleanup();
  213. return 0;
  214. }
复制代码
>>
[C#版]写起来真啰嗦,还需要.NET3.5及以上。
  1. /*************************************************************************
  2. @ECHO OFF&CLS&TITLE MINI CSHARP COMPILER BY HAPPY
  3. for /f "delims=" %%a in ('dir /a-d /b /s "%systemroot%\Microsoft.NET\Framework\v3.5\csc.exe"') do (
  4. "%%~a" /out:a.exe "%~f0" && a.exe
  5. pause&exit/b
  6. )
  7. )
  8. set/p=You needs Microsoft.NET!&exit/b
  9. **************************************************************************/
  10. using System;
  11. using System.Net;
  12. using System.IO;
  13. using System.Text;
  14. using System.Xml;
  15. using System.Runtime.Serialization.Json;
  16. using System.Runtime.Serialization;
  17. using System.Text.RegularExpressions;
  18. namespace GuessNumApplication
  19. {
  20. public class MainProgram
  21. {
  22. static void Main(string[] args)
  23. {
  24. GuessObj guessObj = new GuessObj("adad", "123", "http://www.codetiger.win/extra/API.php");
  25. }
  26. }
  27. public class RespData
  28. {
  29. public long tokens{get; set;}
  30. public int A{get; set;}
  31. public int B{get; set;}
  32. public int count{get; set;}
  33. }
  34. public class GuessObj
  35. {
  36. private static readonly int MAX_ARRAY_SIZE = 5040;
  37. private bool[] markPM = new bool[MAX_ARRAY_SIZE];
  38. public GuessObj(string userName, string passWord, string hostURL)
  39. {
  40. GuessNumber(userName, passWord, hostURL);
  41. }
  42. private string[] Permutations()
  43. {
  44. string[] pmArray = new string[MAX_ARRAY_SIZE];
  45. int index=0;
  46. for(int i=0; i<10; i++){
  47. for(int j=0; j<10; j++){
  48. for(int k=0; k<10; k++){
  49. for(int l=0; l<10; l++){
  50. if((i-j)*(i-k)*(i-l)*(j-k)*(j-l)*(k-l)!=0){
  51. pmArray[index++] = "" + i + j + k + l;
  52. }
  53. }
  54. }
  55. }
  56. }
  57. return pmArray;
  58. }
  59. private int GetWeights(string correctNum, string guessNum)
  60. {
  61. int retWeights = 0;
  62. for(int i=0; i<4; i++){
  63. for(int j=0; j<4; j++){
  64. if (correctNum[i] == guessNum[j]) {
  65. retWeights += (i==j)?5:1;
  66. }
  67. }
  68. }
  69. return retWeights;
  70. }
  71. private int GuessCore(string[] arrayPM, int arrayPMLen, int responseWeights, string guessNum)
  72. {
  73. int markIndex = -1;
  74. for(int i=1; i<arrayPMLen; i++){
  75. if(markPM[i]){
  76. if(GetWeights(arrayPM[i], guessNum) != responseWeights){
  77. markPM[i] = false;
  78. }else if(markIndex == -1){
  79. markIndex = i;
  80. }
  81. }
  82. }
  83. return markIndex;
  84. }
  85. private object ParseJson(string strJson)
  86. {
  87. DataContractJsonSerializer dJsonSerializer = new DataContractJsonSerializer(typeof(RespData));
  88. MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(strJson));
  89. return dJsonSerializer.ReadObject(memoryStream);
  90. }
  91. private string HttpPostData(string url, string postStr)
  92. {
  93. var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  94. httpWebRequest.ContentType = "application/x-www-form-urlencoded";
  95. httpWebRequest.Method = "POST";
  96. using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
  97. {
  98.     streamWriter.Write(postStr);
  99.     streamWriter.Flush();
  100.     streamWriter.Close();
  101. }
  102. var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  103. using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  104. {
  105.     return streamReader.ReadToEnd();
  106. }
  107. }
  108. private void GuessNumber(string userName, string passWord, string hostURL)
  109. {
  110. string[] pmArray = Permutations();
  111. int pmArrayLen = pmArray.Length;
  112. RespData respData = new RespData();
  113. for(;;){
  114. string responseString = "";
  115. int currentIndex = 0;
  116. long? responseTokens = null;
  117. int guessTimes = 0;
  118. for(int i=0; i<MAX_ARRAY_SIZE; i++){
  119. markPM[i] = true;
  120. }
  121. for(;;){
  122. try{
  123. responseString = HttpPostData(hostURL, "username="+userName +  "&password="+passWord +  "&send=answer" + "&number="+pmArray[currentIndex]);
  124. respData =ParseJson(responseString) as RespData;
  125. }catch{
  126. Console.WriteLine("Requests error!!!");
  127. break;
  128. }
  129. guessTimes ++;
  130. if(respData.A == 4){
  131. Console.WriteLine("Takes:{0}th, Number:{1}, Score:{2}", guessTimes, pmArray[currentIndex], respData.count);
  132. break;
  133. }
  134. if(responseTokens == null){
  135. responseTokens = respData.tokens;
  136. }else if(responseTokens != respData.tokens){
  137. Console.WriteLine("Reset...");
  138. break;
  139. }
  140. if( (currentIndex=GuessCore(pmArray, pmArrayLen, respData.A*5+respData.B, pmArray[currentIndex])) == -1){
  141. Console.WriteLine("Fault...");
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
复制代码
>>
[php猜数字服务端实现]可用它在本地模拟猜数字游戏。
  1. <?php
  2. header("Cache-Control: no-cache, must-revalidate");
  3. function digital4s_rand(){
  4. $rdigital = array();
  5. while ($i<4) {
  6. $rdigital[] = mt_rand(0, 9);
  7. $rdigital = array_flip(array_flip($rdigital));
  8. $i = count($rdigital);
  9. }
  10. shuffle($rdigital);
  11. return implode('', $rdigital);
  12. }
  13. if ($_POST['send'] == 'answer') {
  14. if (!empty($_POST['number'])) {
  15. $inputNumber =  (string)$_POST['number'];
  16. $json=Json_decode(file_get_contents("server.sdb"),true);
  17. $count=$json['count'];
  18. $tokens=$json['tokens'];
  19. $correctNumber =$json['number'];
  20. $A=0; $B=0;
  21. for ($i=0; $i<4; $i++) {
  22. for ($j=0; $j<4; $j++) {
  23. if ($inputNumber[$i] === $correctNumber[$j]){
  24. ($i==$j) ?$A++ :$B++;
  25. }
  26. }
  27. }
  28. if($A == 4){
  29. $json['number'] = digital4s_rand();
  30. $json['tokens'] = (string)mt_rand(100000000, 200000000);
  31. $json[ 'count'] = $count + 1;
  32. file_put_contents("server.sdb",json_encode($json));
  33. }
  34. }
  35. die(json_encode(array("code"=>200, "A"=>$A, "B"=>$B, "count"=>$json['count'], "tokens"=>$tokens)));
  36. }else{
  37. ?>
  38. <html>
  39. <head>
  40. <title>phpinfo</title>
  41. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  42. <style type="text/css">
  43. body {
  44. background-color: #fff;
  45. color: #222;
  46. font-family: sans-serif;
  47. }
  48. pre {
  49. margin: 0;
  50. font-family: monospace;
  51. }
  52. a:hover {
  53. text-decoration: underline;
  54. }
  55. table {
  56. border-collapse: collapse;
  57. border: 0;
  58. width: 934px;
  59. box-shadow: 1px 2px 3px #0D2B2D;
  60. }
  61. .center {text-align: center;}
  62. .center table {margin: 1em auto;  text-align: left;}
  63. .center th {text-align: center !important;}
  64. td, th {
  65. border: 1px solid #666;
  66. font-size: 100%;
  67. vertical-align: baseline;
  68. padding: 4px 5px;
  69. }
  70. h1 {font-size: 150%;}
  71. h2 {font-size: 125%;}
  72. .p {text-align: left;}
  73. .e {
  74. background-color: #AAC1D4;
  75. width: 300px;
  76. font-weight:normal;
  77. }
  78. .h {
  79. background-color: #AAC1D4;
  80. font-weight: normal;
  81. }
  82. .alink{
  83. color: #000000;
  84. text-decoration: none;
  85. }
  86. .v {
  87. background-color: #ddd;
  88. max-width: 300px;
  89. overflow-x: auto;
  90. }
  91. .v i {color: #999;}
  92. img {display:none;}
  93. hr {
  94. width: 934px;
  95. background-color: #ccc;
  96. border: 0;
  97. height: 1px;
  98. }
  99. </style>
  100. </head>
  101. <body>
  102. <h1>APMA</h1>
  103. <table style="width:365px;background-color: #AAC1D4;>
  104. <tr class="h">
  105. <td>
  106. <a class="alink" href="expand.php">APMA<br>Expand Information</a>
  107. </td>
  108. <td>
  109. <a class="alink" href="adminer">Adminer<br>Database Manager</a>
  110. </tr>
  111. </table>
  112. <?php
  113. ob_start();
  114. phpinfo();
  115. $i = ob_get_contents();
  116. ob_end_clean();
  117. echo ( str_replace ( "module_Zend Optimizer", "module_Zend_Optimizer", preg_replace ( '%^.*<body>(.*)</body>.*$%ms', '$1', $i ) ) ) ;
  118. ?>
  119. </body>
  120. </html>
  121. <?php }?>
复制代码

作者: bbaa    时间: 2017-8-9 22:39

我还活着,回归时间未知 数独计划延期[有点事情]
猜数坏了,暂时修不了【可能代码不严谨产生的复制问题】
作者: 老刘1号    时间: 2017-8-9 22:44

回复 2# bbaa


    发现窥屏er x1
作者: happy886rr    时间: 2017-8-9 22:46

回复 2# bbaa
ok,收到,延期不要紧,只要有创新就好玩。
作者: happy886rr    时间: 2017-8-9 22:57

回复 3# 老刘1号
小站有私货,不错。
作者: 老刘1号    时间: 2017-8-10 00:19

回复 5# happy886rr


    感谢支持,偶尔发些小作品
作者: 523066680    时间: 2017-8-10 16:11

66666
  1. adad       66666 + 0     0.0/min
  2. vic3       54761 + 0     0.0/min
  3. coadsa       839 + 0     0.0/min
  4. llsd         139 + 0     0.0/min
  5. sfwfssf        0 + 0     0.0/min
复制代码

作者: happy886rr    时间: 2017-8-10 19:45

回复 7# 523066680
一起6
作者: 523066680    时间: 2017-8-10 20:21

本帖最后由 523066680 于 2017-8-10 21:56 编辑

回复 8# happy886rr

一、
    我还在折腾那个树的生成,但是遇到一些问题,
方案和百科中的 “反馈个数指标” 方案类似,但是平均猜数次数和里面的统计结果不同,懵逼了,还在排查。
  1. times: 26991, rate: 5.355357
  2. $times_elements = {
  3.   '1' => 1,
  4.   '5' => 1993,
  5.   '8' => 12,
  6.   '6' => 1879,
  7.   '7' => 390,
  8.   '3' => 109,
  9.   '2' => 13,
  10.   '4' => 643
  11. };
复制代码
百度统计的是 5.308,每种回合数的统计也不一样

=================================
二、
如果不使用结构树,每次都遍历筛选,哪怕是索引操作,消耗也蛮大的,我写的C,测完5040种情况,去掉printf, 需要2.8秒。

=================================
三、
同样是筛选法,同样是5040种情况去测试,以不同的起点开始,平均回合数是不一样的。
0123 -> 5.560317
6789 -> 5.593254
不过,这大概和筛选数组后选取不同起点的区别类似。比如选择筛选后的最后一个、第一个排列,结果是不一样的。
作者: happy886rr    时间: 2017-8-10 22:37

本帖最后由 happy886rr 于 2017-8-10 22:39 编辑

回复 9# 523066680
我觉得要缩小数组的规模去筛选。
可以使用位筛法。这样降到5040/8的小规模,然后C语言的内存读取就会加快。效率就上去了。
作者: 523066680    时间: 2017-8-11 19:13

本帖最后由 523066680 于 2017-8-11 19:19 编辑

批处理的话可以用这种方式(借wget 或者curl)获取返回值
  1. wget -O res.txt "http://www.codetiger.win/extra/API.php?username=***&password=***&number=1234&send=answer"
复制代码
返回示例:res.txt
{"code":150,"A":0,"B":1,"count":141878,"tokens":"6257936998656230"}

搜索树也可以用类似哈希的方式,事先初始化一系列变量,
TREE[0123][01]=0124
TREE[ORDER1][AB1][ORDER2][AB2]=...

这样的话处理过程就像是查表法。但是终究无力再写批处理,罢了。
作者: 523066680    时间: 2017-8-11 20:35

最后手痒还是写了,像这样:

struct.bat
  1. @echo off
  2. set tree=0123
  3. set tree[0123][04]=1032
  4. set tree[0123][04][1032][04]=2301
  5. set tree[0123][04][1032][04][2301][22]=2310
  6. set tree[0123][04][1032][04][2301][22][2310][04]=3201
  7. set tree[0123][04][1032][04][2301][04]=3210
  8. set tree[0123][04][1032][22]=1230
  9. set tree[0123][04][1032][22][1230][13]=1302
  10. set tree[0123][04][1032][22][1230][13][1302][04]=2031
  11. set tree[0123][04][1032][22][1230][04]=3012
  12. set tree[0123][21]=0134
复制代码
批处理调用 struct.bat 为变量赋值。借 wget 向网站 post 并获取反馈信息。
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. echo Tree building ...
  4. call struct.bat
  5. echo Begin ...
  6. :AGAIN
  7.     set guess=0123
  8.     set varname=tree
  9.     set prev=0
  10. :WHILE
  11.     call :post %guess%
  12.     echo %guess% - %AB%, %tokens%
  13.     if "%AB%" == "40" (
  14.         echo "Success"
  15.         goto :AGAIN
  16.     )
  17.     if not "%prev%" == "0" (
  18.         if not "%prev%" == "%tokens%" (
  19.             echo "tokens changed";
  20.             goto :AGAIN
  21.         )
  22.     )
  23.     set varname=%varname%[%guess%][%AB%]
  24.     set guess=!%varname%!
  25.     set prev=%tokens%
  26. goto WHILE
  27. :post
  28. wget --quiet -O res.txt "http://www.codetiger.win/extra/API.php?username=BatchScript&password=batchscript&number=%1&send=answer" 2>nul
  29. for /f "tokens=4,6,10 delims=:,}" %%a in (res.txt) do (
  30.     set AB=%%a%%b
  31.     set tokens=%%c
  32. )
  33. goto :eof
复制代码
猜题速度(很慢,但是感觉好玩)
  1. adad       193791 + 0     0.0/min
  2. vic3       142669 + 0     0.0/min
  3. coadsa       850 + 0     0.0/min
  4. BatchScript   752 + 55    109.6/min
  5. llsd         139 + 0     0.0/min
  6. s**sdf      75 + 0     0.0/min
  7. sfwfssf        0 + 0     0.0/min
复制代码
完整的 struct.bat 和 猜数字批处理,已上传附件:
作者: happy886rr    时间: 2017-8-11 20:52

本帖最后由 happy886rr 于 2017-8-11 20:53 编辑

回复 13# 523066680
批处理也只能查表了,因为5000次循环批处理就要好几秒。
你的思路完美的解决了批处理的效率问题,100/min已经是极限了,毕竟是批处理嘛。第三方调用也很耗时的。
作者: ivor    时间: 2017-8-17 10:59

支持一下
作者: 523066680    时间: 2017-8-18 20:01

本帖最后由 523066680 于 2017-8-23 12:04 编辑

本着坚持不懈的精神,终于把平均指标也跑出来了……
  1. Times: 26551, average: 5.268056
  2. 1, 1
  3. 2, 4
  4. 3, 59
  5. 4, 574
  6. 5, 2430
  7. 6, 1885
  8. 7, 87
复制代码

作者: happy886rr    时间: 2017-8-18 20:48

回复 17# 523066680

这个结果跟百度上写的一样,是个概率值。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2