Last active
January 3, 2016 00:19
-
-
Save buptpatriot/8381874 to your computer and use it in GitHub Desktop.
约瑟夫问题
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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