2007. 6. 28.

자리올림 세기

박지인님의 알고리즘을 사용했다.
이 프로그램은 두 값을 합하는 경우 발생하는 자리올림의 갯수를 센다.

(count-carry 1 999)
-> 3

(count-carry 123 456)
-> 0

(defun digit-check (x)
  (let ((modulos (mod x 10)))
    (cond ((= modulos 0) 10)
      (t modulos))))

(defun count-digit (x)
  (cond ((<= x 9) x)
    ((= x 0) 10)
    (t
     (+
      (digit-check x)
      (floor(count-digit (/ x 10)))))))

(defun count-carry (x y)
  (abs (- (+ (count-digit x) (count-digit y)) (count-digit (+ x y)))))

댓글 없음:

댓글 쓰기