-----
In this case, you'd probably construct a portfolio with some shares (but not high risk ones), along with gilts, cash deposits,
and perhaps convertibles or the income shares of split capital investment trusts.
-----
继续昨天的问题,在文章里不断优化解法之下,有如下的例子。
List 11 : パズル「小町虫食い算」の解法(高速バージョン)
; 2つの数値を生成する
(defun solve-very-fast (&optional (n 0) (numbers '(1 2 3 4 5 6 7 8 9)) (value 0) (abc 0))
(if (= n 3)
(if (and (oddp value) (< abc value))
(if (plusp abc)
(check-fast abc value numbers)
(solve-very-fast 0 numbers 0 value)))
(dolist (x numbers)
(solve-very-fast (1+ n) (remove x numbers) (+ (* 10 value) x) abc))))
; 小町算の条件を満たすかチェックする
(defun check-fast (abc def numbers)
(let ((ghi (- 999 abc def)) n)
(when (< def ghi)
(dotimes (x 3 (format t "~D + ~D + ~D = 999~%" abc def (- 999 abc def)))
(setq n (mod ghi 10)
ghi (truncate ghi 10))
(if (member n numbers)
(setq numbers (remove n numbers))
(return))))))
在里面有几个地方蛮新鲜的,标注一下来记录。
1) (plusp x) 是判断是否为正的函数
2) (truncate x y) 求出用 y 整除 x 的商和余数
3) (dotimes (x 3 (z)) ...) 运行完 x 的 3 以后,执行 z 里面的操作,这个一直不用就不记得了。今天看到之后想了一会儿才想起来。
4) 用&optional和函数的递归来替代变量的赋值,让内部的函数都是一行一行的整洁干净。