C++ 用模数法解决换币问题

C++ 用模数法解决换币问题,c++,mod,coin-change,C++,Mod,Coin Change,我正在寻找一种不同的方法来解决使用模数的硬币兑换问题。大多数解决方案都是使用动态内存来解决这个问题 Example: You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amoun

我正在寻找一种不同的方法来解决使用模数的硬币兑换问题。大多数解决方案都是使用动态内存来解决这个问题

Example: 

You are given coins of different denominations and a total amount of
money amount. Write a function to compute the fewest number of coins
that you need to make up that amount. If that amount of money cannot be
made up by any combination of the coins, return -1.

Input: coins = [1, 2, 5], amount = 11
Output: 3 
Explanation: 11 = 5 + 5 + 1
我们的目标是使用模数创建一个解决方案

这是我到目前为止所做的尝试。我想知道我的变量是否应该初始化为0以外的值,或者我在代码块的错误部分进行了更新

class Solution {
public:

    int coinChange(vector<int>& coins, int amount) {

        int pieces = 0;
        int remainder = 0;

        for(int i = coins.size()-1; i = 0; i--) {
            if (amount % coins[i] == 0)
            {
                pieces += amount/coins[i];
            } else {
                pieces += amount/coins[i];
                remainder = amount%coins[i];
                amount = remainder;
            }
        }
        return pieces;
    }
}
类解决方案{
公众:
整数硬币兑换(矢量和硬币,整数金额){
整数=0;
整数余数=0;
对于(int i=coins.size()-1;i=0;i--){
如果(金额%coins[i]==0)
{
件数+=金额/硬币[i];
}否则{
件数+=金额/硬币[i];
余数=金额%硬币[i];
金额=剩余金额;
}
}
返回件;
}
}

我期望输出如上所述。卡住了,不知道还有什么可以让它工作

我理解您想要做什么,但您的代码实际上并不能实现您认为可以实现的目标。以下是您的代码明细:

int coinChange(vector<int>& coins, int amount) {

    // Minimum number of coins to sum to 'amount'
    int pieces = 0;
    int remainder = 0;

    // Assuming 'coins' is a non-decreasing vector of ints,
    // iterate over all coins, starting from the larger ones,
    // ending with the smaller ones. This makes sense, as it
    // will use more coins of higher value, implying less
    // coins being used
    for(int i = coins.size()-1; i = 0; i--) {
        // If what's left of the original amount is
        // a multiple of the current coin, 'coins[i]',
        if (amount % coins[i] == 0)
        {
            // Increase the number of pieces by the number
            // of current coins that would satisfy it
            pieces += amount/coins[i];

            // ERROR: Why are you not updating the remaining amount?
        } else {
            // What's left of the original amount is NOT
            // a multiple of the current coin, so account
            // for as much as you can, and leave the remainder
            pieces += amount/coins[i];
            remainder = amount%coins[i];
            amount = remainder;
        }
    }

    // ERROR: What if amount != 0? Should return -1
    return pieces;
}
int硬币兑换(矢量和硬币,整数金额){
//总计为“金额”的最小硬币数量
整数=0;
整数余数=0;
//假设“硬币”是整数的非递减向量,
//迭代所有硬币,从较大的硬币开始,
//以较小的结尾。这是有道理的,因为
//将使用更多价值更高的硬币,意味着更少
//正在使用的硬币
对于(int i=coins.size()-1;i=0;i--){
//如果原始金额剩下的是
//当前硬币的倍数,“硬币[i]”,
如果(金额%coins[i]==0)
{
//将件数增加一倍
//可以满足它的流通硬币的数量
件数+=金额/硬币[i];
//错误:为什么不更新剩余金额?
}否则{
//原始金额的剩余部分不是
//当前硬币的倍数,所以账户
//尽你所能,留下剩下的
件数+=金额/硬币[i];
余数=金额%硬币[i];
金额=剩余金额;
}
}
//错误:如果金额!=0怎么办?应返回-1
返回件;
}
如果您修复了我上面提到的
错误
s,则假定
硬币
中的所有整数的行为如下:

  • 如果一枚硬币
    s
    比另一枚硬币
    l
    小,那么
    l
    必须是
    s
    的倍数
  • 每枚硬币必须大于等于1
  • 1的证明:

    如果一枚硬币
    s
    比另一枚硬币小,
    l
    ,但
    l
    不是
    s
    的倍数,那么在解决方案中使用
    l
    作为硬币之一可能是个坏主意。让我们考虑一个例子,其中<代码>硬币= [ 4, 7 ] < /代码>,和<代码>金额=8 < /代码>。您将以非递增顺序迭代
    硬币
    ,从
    7开始
    7
    适合
    8
    ,因此您会说
    碎片=1
    ,而
    金额=1
    保留。现在,
    4
    不适合
    金额,所以您不添加它。现在for循环结束,
    amount!=0,因此函数失败。然而,一个有效的解决方案是两枚
    4
    硬币,因此返回
    碎片=2

    证明2:

    如果一枚硬币,
    c
    小于1,它可以是0或更小。如果
    c
    为0,您将除以0并抛出一个错误。更令人困惑的是,如果您更改代码,您可以添加价值为0的无限量硬币


    如果<代码> c>代码>为负,则将用一个负数除以,导致一个负数,破坏逻辑。

    无关:考虑使用反迭代器代替< <代码> > < /Calp>Road中的索引。逻辑更简单一点,通过了解迭代器是什么,你会让面试官大吃一惊。你描述的是一种贪婪的方法来解决这个问题,对于不同于美元的面额来说并不总是正确的。例如,给定面额
    {1,5,8,10}
    和总金额13,当存在
    {8,5}
    的更优解决方案时,您的算法可能会将硬币
    {10,1,1}
    用于总共4枚硬币。哦,好的,我明白您的意思了。谢谢。您已经描述了代码的目标,包含了代码,并提供了预期的输出。这是好的(这比许多问题都要多)。然而,您缺少的一个关键部分是您的实际输出。如果没有这些,我就假设您得到了预期的输出,因此没有问题要回答。如果您正在寻找一个能够产生正确结果的递归解决方案,请参考几周前我为Python提供的解决方案。(由于我最初犯的一个错误,它最初获得了一些反对票——但现在它起作用了)。这是一个易于C++的端口。