最大币值问题

public class coinmax {
	static int[] c = {1,5,10,20,50,100};
	static int n=c.length-1;
    static int[] a = new int[n+1];
    static int[] f = new int[n+1];
    static int[] arr = new int[n+1];
 
    public static void main(String[] args) {
       
 
        
 
        System.out.print("递归法求得结果为:" + fun1(n));
 
        System.out.print("\n备忘录法求得结果为:" + fun2(n));
 
        System.out.print("\n动态规划法求得结果为:");
        coinRow(n);
        System.out.println(f[n ]);
 
        int count = 0;
        for (int i = n; i >= 1 ; i--) {
            if (f[i] == f[i - 1])
            {
                arr[count++] = c[--i];
            } else {
                arr[count++] = c[i--];
            }
        }
        System.out.println("拿的面额分别为");
        for (int i = n; i >= 0; i--) {
            if(arr[i] > 0)
                System.out.print(arr[i] + "\t");
        }
    }
 
    
 
    //普通递归求
    static int fun1(int n) {
        if (n == 0)
            return c[0];
        if (n == 1)
            return Math.max(c[0], c[1]);
        else
            return Math.max(c[n] + fun1(n - 2), fun1(n - 1));
    }
 
    //备忘录
    static int fun2(int n) {
        if (n == 0)
            return c[0];
        if (n == 1)
            return Math.max(c[0], c[1]);
        else if (a[n] != 0)
            return a[n];
        return a[n] = Math.max(c[n] + fun2(n - 2), fun2(n - 1));
    }
 
    //动态规划法求
    static void coinRow(int n) {
        f[0] = 0;
        f[1] = c[1];
 
        for (int i = 2; i <= n; i++) {
            f[i] = Math.max(c[i] + f[i-2], f[i-1]);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值