算法设计与分析: 3-2 最少硬币问题

本文介绍了最少硬币找零问题,包括问题描述和两种解决方案:深度优先搜索遍历法(DFS)及动态规划法。提供了Java代码实现,帮助理解算法设计与分析。
摘要由CSDN通过智能技术生成

3-2 最少硬币问题


问题描述

设有n种不同面值的硬币,各硬币的面值存于数组T[1:n]中。现要用这些面值的硬币来找钱。可以使用的各种面值的硬币个数存于数组Coins[1:n]中。对任意钱数0≤m≤20001,设计一个用最少硬币找钱m的方法。
对于给定的1≤n≤10,硬币面值数组T和可以使用的各种面值的硬币个数数组Coins,以及钱数m,0≤m≤20001,编程计算找钱m的最少硬币数。


深度优先搜索遍历法(DFS)

Java

public class ZuiShaoYingBi {
   
    //case 1
    private static int n = 3;
    private static int[] T = {
  1, 2, 5};
    private static int[] coins = {
  3, 3, 3};

    //case 2
//    private static int n = 3;
//    private static int[] T = {19, 20, 50};
//    private static int[] coins = {3, 3, 3};

    //case 3
//    private static int n = 4;
//    private static int[] T = {1, 2, 5, 18};
//    private static int[] coins = {3, 3, 3, 1};

    //case 4
//    private static int n = 4;
//    private static int[] T = {1, 2, 5, 18};
//    private static int[] coins = {3, 3, 3, 0};

    private static int m = 18;
//    private static int m = 19;
//    private static int m = 20;
//    private static int m = 21;
//    private static int m = 22;
//    private static int m = 23;
//    private static int m = 24;
//    private static int m = 25;
//    private static int m = 0;
//    private static int m = 1;
    private static int coinsCount = Integer.M
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值