Skip to content

Instantly share code, notes, and snippets.

@buptpatriot
Last active January 3, 2016 00:19
Show Gist options
  • Save buptpatriot/8381874 to your computer and use it in GitHub Desktop.
Save buptpatriot/8381874 to your computer and use it in GitHub Desktop.
约瑟夫问题
(define (f n k)
(define (f-iter result counter)
(if (> counter n)
result
(f-iter (add1 (modulo (+ result k -1) counter))
(add1 counter))))
(f-iter 1 2))
(define (advanced-f prisoners k)
(define (new-list lst p)
(append (list-tail lst (add1 p))
(take lst p)))
(let* ((len (length prisoners))
(pos (modulo (- k 1) len)))
(if (= len 1)
(display prisoners)
(begin
(display (list-ref prisoners pos))
(display " ")
(advanced-f (new-list prisoners pos) k)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment