0010 嘿嘿

发表失败,草稿箱里面也没有。。。。。。?!
-----
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值