MoneyWays

public class MoneyWays {

    /**
     * 面值为:3,5,7,1  f
     * 每个面值可以用无数张
     * 100
     * 从左往右尝试
     * f(index,res)
     * 0张3元的 f(index+1,res)+
     * 1张3元的 f(index+1,res-3*1)+
     * 33........f(index+1,res-3*33)+
     */
    public int moneyWays(int[] arr,int aim){
        if(arr==null||arr.length==0||aim<0){
            return -1;
        }
        return process(arr,0,aim);
    }

    public int process(int[] arr,int index,int res){
        //base case 无面值的时候
        if(index==arr.length){
            //到最后了,有没有组成目标值
            return res==0?1:0;
        }

        int ways=0;
        for(int zhang=0;zhang*arr[index]<=res;zhang++){
           ways+=process(arr,index+1,res-zhang*arr[index]);
        }
        return ways;

    }
}

  

posted @ 2021-09-05 16:29  sherry001  阅读(19)  评论(0编辑  收藏  举报