请问这段代码有什么问题,最终结果输入是nil
(defun stair-level-steps (step-num / stair-level remainder stair-level-steps new-remainder j stair-level-list stair-level-steps new-stair-level-list temp)(setq stair-level (fix (/ step-num 18)) ;; 计算 step-num 除以 18 的商,存储在 b
remainder (rem step-num 18 ) ;; 计算 n 减去 n 除以 18 的商乘以 18 的结果,存储在 remainder
stair-level-steps nil ;; 创建一个空列表 result-list
)
(if (zerop remainder)
(repeat (fix stair-level)
(setq result-list (appendresult-list (list 18)))
)
(progn
(setq stair-level-steps (fix (/ step-num (+ stair-level 1)))
new-remainder (fix (- step-num (* (+ stair-level 1) stair-level-steps)))
j 0
stair-level-list nil
new-stair-level-listnil
temp nil
)
(repeat (fix (+ 1 stair-level))
(setq stair-level-list (appendstair-level-list (list stair-level-steps)))
)
(while (> new-remainder 0)
(repeat new-remainder
(setq temp (nth j stair-level-list)
temp (+ 1 temp)
new-stair-level-list (append new-stair-level-list (list temp))
j (1+ j)
)
)
(reverse new-stair-level-list)
)
)
)
)
(defun c:tt()
(stair-level-steps (getreal "\n 请输入级数"))
)各路大神,请问这段代码有什么问题,为啥输入的结果是nil
(defun stair-steps (step-num / stair-level remainder stair-level-steps new-remainder j stair-level-list stair-level-steps temp new-stair-level-list)
(setq stair-level (fix (/ step-num 18)) ;; 计算 step-num 除以 18 的商,存储在 b
remainder (rem step-num 18 ) ;; 计算 n 减去 n 除以 18 的商乘以 18 的结果,存储在 remainder
stair-level-steps nil ;; 创建一个空列表 result-list
new-stair-level-list nil
)
(if (zerop remainder)
(repeat (fix stair-level)
(setq stair-level-steps (appendstair-level-steps (list 18)))
)
(progn
(setq stair-level-steps (fix (/ step-num (+ stair-level 1)))
new-remainder (fix (- step-num (* (+ stair-level 1) stair-level-steps)))
j 0
stair-level-list nil
temp nil
temp2 nil
)
(if(zerop new-remainder)
(repeat (fix (+ 1 stair-level))
(setq stair-level-list (appendstair-level-list (list stair-level-steps)))
)
(progn
(repeat (fix (+ 1 stair-level))
(setq stair-level-list (appendstair-level-list (list stair-level-steps)))
)
(repeat new-remainder
(setq temp (nth j stair-level-list)
temp (+ 1 temp)
stair-level-list (append stair-level-list (list temp))
j (1+ j)
)
)
(setq stair-level-list-length (length stair-level-list)
i (- stair-level-list-length (+ 1 stair-level))
)
(repeat (+ stair-level 1)
(setq
temp2 (nth i stair-level-list)
new-stair-level-list (append new-stair-level-list (list temp2))
i (1+ i)
)
)
(reverse new-stair-level-list)
)
)
)
)
)用了最笨的办法解决了{:1_1:}有没有帮忙优化的,或者提供个思路 (defun c:tt ()
(defun stair (int / a aa bb lst n nn)
(setq int (if (/= int (fix int))
(1+ (fix int))
(fix int)
)
aa(fix (/ int 18)) ; 最多18步
bb(rem int 18) ; 余数
)
(if (= bb 0)
(repeat aa (setq lst (cons 18 lst)))
(progn
(setq nn (1+ aa)
n(/ int nn 1.)
n(if (/= n (fix n))
(1+ (fix n))
(fix n)
)
)
(while (> int n)
(setq lst (cons n lst)
int (- int n)
nn(1- nn)
a (/ int nn 1.)
)
(if (= a (fix a))
(repeat nn
(setq lst (cons (fix a) lst)
int (- int (fix a))
)
)
)
)
(reverse lst)
)
)
)
(stair (getint "\n请输入级数: "))
)
xyp1964 发表于 2024-9-8 16:25
哇!谢谢版主 本帖最后由 lijiao 于 2024-9-9 15:08 编辑
给你简化了一下
(defun stair-steps (step-num / AVE LST M REMAINDER STAIR-LEVEL X)
(setq step-num (fix step-num)
stair-level (if (> (rem step-num 18) 0)
(1+ (/ step-num18))
(/ step-num18)
)
)
(setq ave (fix (/ step-num stair-level))
m (- step-num (* ave stair-level)))
(mapcar '(lambda(x)
(repeat (car x)
(setq lst (cons (cadr x) lst))
)
)
(list (list (- stair-level m) ave) (list m (1+ ave)))
)
lst
)
lijiao 发表于 2024-9-9 14:43
给你简化了一下
非常感谢,昨天也重新写了一个(defuntt (stair-steps / len bottom top remainder results)
(setq stair-levels (ceiling (/ stair-steps 18)))
(setq min-stair-levels (fix (/ stair-steps stair-levels)))
(setq max-stair-leves (+ min-stair-levels 1))
(setq remainder (rem stair-steps min-stair-levels))
(setq stair-steps-list nil)
(repeat stair-levels
(if (>= (+ remainder (length stair-steps-list)) stair-levels)
(setq stair-steps-list (append stair-steps-list (list max-stair-leves)))
(setq stair-steps-list (append stair-steps-list (list min-stair-levels)))
)
)
(reverse stair-steps-list)
)
(defun ceiling (x)
(if (= x (fix x))
(fix x)
(+ 1 (fix x))
)
) 天之如一 发表于 2024-9-9 15:24
非常感谢,昨天也重新写了一个
(tt 55) → (19 18 18)
明显错误 (defun stair-steps (int / av lst m nn x)
;; (stair-steps 59) → (15 15 15 14)
;; (stair-steps 60) → (15 15 15 15)
(setq int (fix int)
nn(/ int 18)
nn(if (> (rem int 18) 0)(1+ nn)nn)
av(fix (/ int nn))
m (- int (* av nn))
)
(repeat (- nn m) (setq lst (cons av lst)))
(repeat m (setq lst (cons (1+ av) lst)))
lst
) 这个帖子很有意思,它的实质是一个鸡兔同笼问题
页:
[1]