//  Copyright 2016 The Sawdust Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package backtracing;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class ChangeMoney {
    private int[] currencys;
    private int solutonsNum;

    public ChangeMoney(int[] currencys) {
        this.currencys = currencys;
    }
    
    private void select(int from, int target, List solution) {
        for (int i = from; i < currencys.length; i++) {
            int v = currencys[i];
            int nextTarget = target - v;
            if (nextTarget == 0) {
                solution.add(currencys[i]);
                solutonsNum++;
                System.out.println(solution.toString());

                solution.remove(solution.size() - 1);
                return;
            }
            if (nextTarget < 0) {
                return;
            }
            solution.add(currencys[i]);
            select(i, nextTarget, solution);
            solution.remove(solution.size() - 1);
        }
    }

    public void changeWays(int target) {
        Arrays.sort(currencys);
        solutonsNum = 0;

        List<Integer> solution = new LinkedList<>();
        select(0, target, solution);

        System.out.println("in total:" + solutonsNum);
    }

    public static void main(String[] args) {
        ChangeMoney cm = new ChangeMoney(new int[]{1, 2, 5});
        cm.changeWays(4);
    }
}

@@ -706,16 +706,53 @@
 .List;%0A%0A
+/**%0A * Thanks a lot to Wen, Jie.%0A */%0A
 public c
@@ -913,20 +913,16 @@
 ;%0A    %7D%0A
-    
 %0A    pri
@@ -937,16 +937,26 @@
 d select
+WithDetail
 (int fro
@@ -1508,16 +1508,26 @@
   select
+WithDetail
 (i, next
@@ -1604,32 +1604,442 @@
        %7D%0A    %7D%0A%0A
+%0A    private void select(int from, int target) %7B%0A        for (int i = from; i %3C currencys.length; i++) %7B%0A            int v = currencys%5Bi%5D;%0A            int nextTarget = target - v;%0A            if (nextTarget == 0) %7B%0A                solutonsNum++;%0A                return;%0A            %7D%0A            if (nextTarget %3C 0) %7B%0A                return;%0A            %7D%0A%0A            select(i, nextTarget);%0A        %7D%0A    %7D%0A%0A%0A
     public void 
@@ -2051,32 +2051,52 @@
 eWays(int target
+, boolean withDetail
 ) %7B%0A        Arra
@@ -2141,16 +2141,46 @@
 m = 0;%0A%0A
+        if (withDetail) %7B%0A    
         
@@ -2232,22 +2232,36 @@
         
+    
 select
+WithDetail
 (0, targ
@@ -2274,16 +2274,74 @@
 lution);
+%0A        %7D else %7B%0A            select(0, target);%0A        %7D
 %0A%0A      
@@ -2392,24 +2392,25 @@
 um);%0A    %7D%0A%0A
+%0A
     public s
@@ -2504,16 +2504,17 @@
 2, 5%7D);%0A
+%0A
         
@@ -2531,9 +2531,48 @@
 ays(
-4
+5, true);%0A        cm.changeWays(5, false
 );%0A 

Provide with-detail option in ChangeMoney