Representing an amount of money with specific bills

后端 未结 4 503
谎友^
谎友^ 2020-12-21 17:16

I want to write a function in Racket which takes an amount of money and a list of specific bill-values, and then returns a list with the amount of bills used of every type t

4条回答
  •  既然无缘
    2020-12-21 17:26

    I solved it this way now :)

    (define (calc n xs)
      (define (calcAssist n xs usedBills)
        (cond ((null? xs) usedBills)
              ((pair? xs) 
                (calcAssist (- n (* (car xs) (floor (/ n (car xs))))) 
                            (cdr xs) 
                            (append usedBills 
                                    (list (floor (/ n (car xs)))))))
              (else 
                (if ((= (- n (* xs (floor (/ n xs)))) 0)) 
                   (append usedBills (list (floor (/ n xs))))
                   (display "No solution")))))
    
      (calcAssist n xs (list)))
    

    Testing:

    > (calc 415 (list 100 10 5 2 1))
    '(4 1 1 0 0)
    

提交回复
热议问题