我試圖用球拍解決這個難題https://puzzling.stackexchange.com/questions/40094/who-committed-the-crime。解決球拍中的謎題
一人犯罪已有5人涉嫌。每個犯罪嫌疑人都被問及他們認爲犯罪的情況。
他們的回答如下:
Terry : It wasn't Carl, It was Steve
Steve : It wasn't Matt, It wasn't Carl
Matt : It was Carl, It wasn't Terry
Ben : It was Matt, It was Steve
Carl : It was Ben, It wasn't Terry
測謊儀顯示, 每個可疑告訴一個謊言,一個道理。誰犯了罪?
以下是我的代碼:
(define (oneof a b)
(or (and a (not b)) (and b (not a))))
(for ((i 5))
(define templist (list #f #f #f #f #f)) ; make a temporary list of all false;
(set! templist (list-set templist i #t)) ; one by one keep one as true in this loop;
(define t (list-ref templist 0)) ; allocate each person according to above list (one kept true one by one)
(define s (list-ref templist 1))
(define m (list-ref templist 2))
(define b (list-ref templist 3))
(define c (list-ref templist 4))
(when ; test if all statements fit with above assignment:
(and
(oneof (not c) s) ; Terry's statement
(oneof (not m) (not c)) ; Steve's statement
(oneof c (not t)) ; Matt's statement
(oneof m s) ; Ben's statement
(oneof b (not t))) ; Carl's statement
(println (list "t" "s" "m" "b" "c")) ; print allocation if all statement fit in;
(println templist)))
輸出指示馬特犯了罪:
'("t" "s" "m" "b" "c")
'(#f #f #t #f #f)
它的工作原理,但代碼是必要的,而不是非常實用(尤其是定義T,定義小號,...部分)。如何改進?感謝您的評論/答覆。
代碼檢查SO將會更適合。 – Sylwester