[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
多线程,实测速度能稳定在 20/min 以上(由网络环境决定)
Java 代码
  1. import com.google.gson.Gson;
  2. import okhttp3.*;
  3. import java.io.IOException;
  4. public class Main {
  5.     private static final OkHttpClient client = new OkHttpClient();
  6.     private volatile static int calls = 0;
  7.     private static int min = -1;
  8.     private static int max = 10001;
  9.     private static String tokens = null;
  10.     private static boolean finished = false;
  11.     private static int times = 0;
  12.     static {
  13.         client.dispatcher().setMaxRequests(Integer.MAX_VALUE);
  14.         client.dispatcher().setMaxRequestsPerHost(Integer.MAX_VALUE);
  15.     }
  16.     public static void main(String[] args) {
  17.         int next;
  18.         int add;
  19.         long start = System.nanoTime();
  20.         while (true) {
  21.             if (calls != 0) continue;
  22.             if (finished) {
  23.                 System.out.println(String.format("%d 次 %fs", times, (System.nanoTime() - start) / 1000000000d));
  24.                 start = System.nanoTime();
  25.                 times = 0;
  26.                 finished = false;
  27.             }
  28.             if (max - min < 10) {
  29.                 for (int i = min + 1; i < max; i++) {
  30.                     guessAsync(i);
  31.                 }
  32.                 continue;
  33.             }
  34.             next = min;
  35.             add = (max - min) / 4;
  36.             for (int i = 0; i < 3; i++) {
  37.                 guessAsync(next += add);
  38.             }
  39.         }
  40.     }
  41.     private static void guessAsync(int num) {
  42.         FormBody body = new FormBody.Builder()
  43.                 .add("send", "Answer")
  44.                 .add("math", String.valueOf(num))
  45.                 .add("username", "Scallop")
  46.                 .build();
  47.         Request request = new Request.Builder().url("http://bbaass.tk/math/").post(body).build();
  48.         client.newCall(request).enqueue(new Callback() {
  49.             @Override
  50.             public void onFailure(Call call, IOException e) {
  51.                 e.printStackTrace();
  52.                 calls--;
  53.             }
  54.             @Override
  55.             public void onResponse(Call call, Response response) throws IOException {
  56.                 synchronized (client) {
  57.                     try {
  58.                         if (finished) {
  59.                             response.close();
  60.                             calls--;
  61.                             return;
  62.                         }
  63.                         if (!response.isSuccessful()) {
  64.                             response.close();
  65.                             System.out.println("---故障---");
  66.                             calls--;
  67.                             return;
  68.                         }
  69.                         BBAAResponse result = new Gson().fromJson(response.body().string(), BBAAResponse.class);
  70.                         response.close();
  71.                         String re = result.re;
  72.                         boolean reset = !result.tokens.equals(tokens) && !re.equals("=");
  73.                         if (reset) {
  74.                             if (!result.winer.equals("Scallop")) {
  75.                                 System.out.println(String.format("---重置 by %s---", result.winer));
  76.                             }
  77.                             tokens = result.tokens;
  78.                         }
  79.                         switch (result.re) {
  80.                             case "<":
  81.                                 if (min < num) min = num;
  82.                                 if (reset) max = 10001;
  83.                                 break;
  84.                             case "=":
  85.                                 min = -1;
  86.                                 max = 10001;
  87.                                 finished = true;
  88.                                 break;
  89.                             case ">":
  90.                                 if (max > num) max = num;
  91.                                 if (reset) min = -1;
  92.                         }
  93.                     } catch (Exception e) {
  94.                         e.printStackTrace();
  95.                     }
  96.                     calls--;
  97.                 }
  98.             }
  99.         });
  100.         calls++;
  101.         times++;
  102.     }
  103.     private static class BBAAResponse {
  104.         int code;
  105.         String re;
  106.         String tokens;
  107.         String winer;
  108.     }
  109. }
复制代码

TOP

返回列表