網頁

2013年11月30日 星期六

Balance partion problem

 (defun balpart (lst)
   "balace partion problem " 
   (let* ((sum (apply #'+ lst))
         (halfsum (floor (/ sum 2)) )
         (sol (make-list (+ halfsum 1) :initial-element nil )) 
         )
     (setf (first sol ) t ) 
     (dolist (i lst ) 
       (do ((j halfsum (decf j)))
          ((< j i))
        (if (nth (- j i) sol)
            (setf (nth j sol) t))))
 
     (let ( (halfcloser (do ((i halfsum (decf i) )) ((nth i sol) i ) )))
       (return-from balpart (- sum halfcloser halfcloser)))
     ))
 
 (format t "Please input a number list: ~%")
 (finish-output t ) 
 (format t "The minimum difference is : ~A~%" (balpart (read) )  )

 
詳細過程請參考此連結

沒有留言:

張貼留言