显示金字塔

今天上了「Java语言程序设计」,讲到了循环,因为对这些基础的非常熟悉,所以提前写完Java版的之后就开始浪CLisp版本的显示金字塔了。

样例输入:5
样例输出:

          1 
        1 2 1 
      1 2 3 2 1 
    1 2 3 4 3 2 1 
  1 2 3 4 5 4 3 2 1 


CLisp版的程序如下

Common Lisp Code

(defvar count-of-rows)
(defun print-line(i)
    (do
        ((space (- count-of-rows i) (decf space)))
        ((< space 0) 0)
        (format t "   ")
    )
    (do
        ((number 1 (incf number)))
        ((> number i) 0)
        (format t "~3d" number)
    )
    (do
        ((number (- i 1) (decf number)))
        ((<= number 0) 0)
        (format t "~3d" number)
    )
    (format t "~%")
)

(defun print-diagram(i)
    (let* ((lines i))
        (do
            ((row 1 (incf row)))
            ((> row i) 0)
            (print-line row)
        )
    )
)

(defun main()
    (format *query-io* "Input number of rows:")
    (force-output *query-io*)
    (setq count-of-rows (read-line *query-io*))
    (setq count-of-rows (parse-integer count-of-rows :junk-allowed t))
    (if count-of-rows
        (print-diagram count-of-rows)
    )
    (force-output *query-io*)
)

(main)

声明: 本文为 Cocoa 原创, 转载注明出处喵~

Leave a Reply

Your email address will not be published. Required fields are marked *