0010 嘿嘿

来源:互联网 发布:无忧商务软件 编辑:程序博客网 时间:2024/05/08 05:25
发表失败,草稿箱里面也没有。。。。。。?!
-----
Once you have decided on your investment aims, you can then decide where to put your money.
The golden rule here is spread your risk, if you put all of your money into Perwigs International, 
you're setting yourself up as a hostage to fortune.
-----
M.Hiroi的这一章讲的是bit操作。正好早上land of lisp里面除了defvar,defparameter,defun之外出现的第一个英语字母函数ash也是bit操作。
用到的实例是开灯游戏。
啊,我打了好多的规则,想法都消失在空气里了。
那么简单的只贴解法。

M.Hiroi的解法:

List 1 : ライツアウトの解法; ボタンを押したときのパターン(defvar *pattern* #(#x0000023 #x0000047 #x000008e #x000011c #x0000218                    #x0000461 #x00008e2 #x00011c4 #x0002388 #x0004310                    #x0008c20 #x0011c40 #x0023880 #x0047100 #x0086200                    #x0118400 #x0238800 #x0471000 #x08e2000 #x10c4000                    #x0308000 #x0710000 #x0e20000 #x1c40000 #x1880000)); 解を求める(defun solve (board)  (dotimes (i 32)    (let ((new-board board) pushed-button)      ; 1 行目のボタンを押す      (dotimes (j 5)        (when (logbitp j i)          (push j pushed-button)          (setq new-board (logxor new-board (aref *pattern* j)))))      ; 1 行ずつライトを消していく      (dotimes (j 20)        (when (logbitp j new-board)          (push (+ j 5) pushed-button)          (setq new-board (logxor new-board (aref *pattern* (+ j 5))))))      ; ライトが全部消えたか      (if (zerop new-board)          (print-answer (reverse pushed-button))))))List 2 : 解の出力(defun print-answer (pushed-button)  (dotimes (x 25 (terpri))    (if (zerop (mod x 5)) (terpri))    (cond ((and pushed-button                (= x (car pushed-button)))           (princ "○")           (pop pushed-button))          (t (princ "×")))))
我扫了一眼之后,自己重新写了一遍:

(defun solve-lamp (board)  (dotimes (i 32)    (let ((new-board board)   operation)      (dotimes (j 5)(when (logbitp j i)  (setf new-board (logxor new-board (aref *pattern* j)))  (push j operation)));      (format t "~X ~%" new-board)      (dotimes (k 4)(dotimes (j 5)  (when (logbitp (+ j (* k 5)) new-board)    (setf new-board (logxor new-board (aref *pattern* (+ j 5 (* k 5)))))    (push (+ j 5 (* k 5)) operation))))      (when (zerop new-board)(print-lamp operation))))) (defun print-lamp (ope)  (format t "~%")  (dotimes (k 5)    (dotimes (j 5)      (if (member (+ j (* k 5)) ope)  (format t "P")  (format t "-")))    (format t "~%")))

主要区别在于:
1)dotimes的关于k和j的循环用单次就可以了
2)输出结果的想法不一样
3)换行不用format 用(terpri)

0 0
原创粉丝点击