兑换零钱

兑换零钱

题目

日本的兑换零钱的机器可以用纸币兑换10日元、50日元、100日元和500日元硬币的组合,且每种硬币的数量都足够多。假设用一张纸币最多只能兑换出15枚这4种硬币,不限组合。

问题

求兑换1000日元纸币时会出现多少种组合?注意,不计硬币兑出的先后顺序。

思路

用穷举法按硬币面额从大到小,简单

    @Test
    public void change() {
        int count = 0;
        for (int i = 0; i <= 2; i++) {//500日元的硬币最多2枚
            for (int j = 0; j <= 10; j++) {//100日元的硬币最多10枚
                for (int k = 0; k <= 15; k++) {
                    for (int l = 0; l <= 15; l++) {
                        if ((500 * i + 100 * j + 50 * k + 10 * l == 1000) && (i + j + k + l) <= 15) {
                            count++;
                        }
                    }
                }
            }
        }
        System.out.println("一共会出现" + count + "种组合");
    }

程序运行结果
一共会出现20种组合
上面的代码把纸币的面额写死在代码里面了,扩展性不好,可以改为递归去求解,代码如下

    public static int zlCount = 0;//记录有多少种组合
    public static int[] ins = {500, 100, 50, 10};//用于找零的硬币

    public void change(int index, int num, int count) {
        if (index == ins.length) {
            return;
        }
        int a = ins[index];
        int current = count / a;
        for (int i = 0; (i <= current && i <= num); i++) {
            if (count == i * a) {
                zlCount++;
            } else {
                change(index + 1, num - i, count - a * i);//递归调用
            }
        }
    }

测试用例

    @Test
    public void getChange() {
        change(0, 15, 1000);
        System.out.println("一共会出现" + zlCount + "种组合");
    }

程序运行结果
一共会出现20种组合

总结

学会使用递归还是很有用的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用递归的方法来列举所有的兑换方式。以下是使用Java代码列举所有的兑换方式: ```java import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { int totalMoney = 10; // 需要兑换的总金额 int[] coins = {1, 5}; // 可用的硬币面额 List<List<Integer>> result = new ArrayList<>(); List<Integer> combination = new ArrayList<>(); findCombinations(totalMoney, coins, 0, combination, result); // 输出所有的兑换方式 for (List<Integer> combinationResult : result) { System.out.println(combinationResult); } } public static void findCombinations(int remainingMoney, int[] coins, int coinIndex, List<Integer> combination, List<List<Integer>> result) { // 当剩余金额为0时,找到一种兑换方式 if (remainingMoney == 0) { result.add(new ArrayList<>(combination)); return; } // 遍历可用的硬币面额 for (int i = coinIndex; i < coins.length; i++) { int coin = coins[i]; // 若当前硬币面额大于剩余金额,则跳过 if (coin > remainingMoney) { continue; } // 加入当前硬币面额到兑换组合中 combination.add(coin); // 递归寻找下一个硬币组合 findCombinations(remainingMoney - coin, coins, i, combination, result); // 移除当前硬币面额,尝试下一个面额 combination.remove(combination.size() - 1); } } } ``` 运行结果如下: ``` [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] [5, 1, 1, 1, 1, 1] [5, 5] ``` 以上代码通过递归的方式列举了所有的兑换方式,每个兑换方式都是一个List<Integer>类型的数组,数组中的元素表示相应面额的硬币数量。在上述结果中,[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] 表示用10个1元纸币兑换,[5, 1, 1, 1, 1, 1] 表示用1个5元纸币和5个1角硬币兑换,[5, 5] 表示用2个5元纸币兑换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值