Java Algorithm to solve vendor machine 'change giving' problem

前端 未结 6 795
太阳男子
太阳男子 2021-01-16 22:10

As a graduate I went for an interview for a java development role and was doing pretty well in the technical examinations until i came up against this question.

If i

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-16 22:46

    Let say your possible coins are x1 ...xn :

    if you're asked to print all the possiblities for $2 then you could print recursively all the possibilities for :

    2-x1
    2-x2
    ..
    2-xn
    

    Yoou will eventually get all the solutions

    You can initialise this recursive process with amount-xi = 0 then print

提交回复
热议问题