[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 TSCAN 于 2017-7-20 10:16 编辑
  1. #lang racket
  2. (require net/http-client json)
  3. (define (reg username)
  4.   (define-values
  5.     (a b c)
  6.     (http-sendrecv "bbaass.tk"
  7.                  "http://bbaass.tk/math/"
  8.                  #:method "POST"
  9.                  #:headers (list "Content-Type: application/x-www-form-urlencoded")
  10.                  #:data (string-append "send=reg&username=" username)))
  11.   (port->string c))
  12. (define ((send-answer username) ans)
  13.   (define-values
  14.     (a b c)
  15.     (http-sendrecv "bbaass.tk"
  16.                  "http://bbaass.tk/math/"
  17.                  #:method "POST"
  18.                  #:headers (list "Content-Type: application/x-www-form-urlencoded")
  19.                  #:data
  20.                  (string-append "send=Answer&username=" username "&"
  21.                                 "math=" (number->string ans) "&")
  22.                                 ))
  23.   c
  24.   )
  25. (define (try-once min max)
  26.   (define (mid x y) (floor (/ (+ x y) 2)))
  27.   (define first-time
  28.                       (read-json ((send-answer "racket")
  29.                       (mid min max)))
  30.                       )
  31.   (define token (hash-ref first-time 'tokens))
  32.   (define first-re (hash-ref first-time 're))
  33.   (define (token-changed? t)
  34.     (not (string=? token t)))
  35.   (define (loop min max exit)
  36.     (printf "min :~a,max :~a\n" min max)
  37.     (define this-time (read-json ((send-answer "racket") (mid min max))))
  38.     (define this-token (hash-ref this-time 'tokens))
  39.     (define this-re (hash-ref this-time 're))
  40.     (when (string=? this-re "=") (exit (printf "succeed.\n")))
  41.     (when (token-changed? this-token) (exit (void)))
  42.     (match this-re
  43.       ["<" (loop (mid min max) max exit)]
  44.       [">" (loop min (mid min max) exit)]))
  45.   (call/ec
  46.    (lambda (exit) (loop min max exit)))
  47.   )
  48. (define (repl) (try-once 0 10000) (repl))
  49. (repl)
复制代码
racket代码,有点慢,注意先用reg函数注册。

更新的代码,速度更快:
  1. #lang racket
  2. (require net/http-client json racket/unsafe/ops)
  3. (define (reg username)
  4.   (define-values
  5.     (a b c)
  6.     (http-sendrecv "bbaass.tk"
  7.                    "http://bbaass.tk/math/"
  8.                    #:method "POST"
  9.                    #:headers (list "Content-Type: application/x-www-form-urlencoded")
  10.                    #:data (string-append "send=reg&username=" username)))
  11.   (port->string c))
  12. (define-syntax-rule (send-answer connection ans)
  13.   (let-values
  14.       [[(a b c)
  15.         (http-conn-sendrecv! connection
  16.                              "http://bbaass.tk/math/"
  17.                              #:method "POST"
  18.                              #:headers (list "Content-Type: application/x-www-form-urlencoded")
  19.                              #:data
  20.                              (string-append "send=Answer&username=racket&"
  21.                                             "math=" (number->string ans) "&")
  22.                              )]]
  23.     c
  24.     ))
  25. ;;Update : No function call here
  26. (define-syntax-rule (mid x y) (if (= 1 (- y x))
  27.                                   y (unsafe-fxquotient (unsafe-fx+ x y) 2)))
  28. (define (try-once)
  29.   (define min 0)
  30.   (define max 10000)
  31.   (define next (mid min max))
  32.   ;;Update : A bug has been fixed.
  33.   (define http-connector (http-conn-open "bbaass.tk" #:auto-reconnect? #t))
  34.   (define first-time
  35.     (read-json (send-answer http-connector
  36.                             (mid min max)))
  37.     )
  38.   (define token (hash-ref first-time 'tokens))
  39.   (define first-re (hash-ref first-time 're))
  40.   (define (token-changed? t)
  41.     (not (string=? token t)))
  42.   
  43.   (define (loop min max)
  44.     (printf "min :~a,max :~a\n" min max)
  45.     (define next (mid min max))
  46.     (define this-time (read-json (send-answer http-connector next)))
  47.     (define this-token (hash-ref this-time 'tokens))
  48.     (define this-re (hash-ref this-time 're))
  49.     (cond
  50.       [(string=? this-re "=") (begin (printf "succeed.\n")
  51.                                      (http-conn-close! http-connector)
  52.                                      )]
  53.       [(token-changed? this-token)  (http-conn-close! http-connector)]
  54.       [(string=? this-re "<") (loop next max)]
  55.       [(string=? this-re ">") (loop min next)]
  56.       
  57.       ))
  58.   ;;Update: Because of tail call optimization, we don't need call/cc or call/ec
  59.   (match first-re
  60.     ["<" (loop next max)]
  61.     [">" (loop min next)]
  62.     [_ (begin (printf "succeed.\n")
  63.               (http-conn-close! http-connector)
  64.               )]
  65.     )
  66.   )
  67.   
  68. (define (repl) (try-once) (repl))
  69. (repl)
复制代码
3

评分人数

TOP

返回列表