📌  相关文章
📜  找出支付总金额所需的最小硬币数量

📅  最后修改于: 2021-05-06 22:16:44             🧑  作者: Mango

给定N的总量和硬币值得1,1025枚货币硬币的数量不受限制。找出您需要用来支付确切金额N的最小硬币数量。

例子:

Input : N = 14
Output : 5
You will use one coing of value 10 and 
four coins of value 1.

Input :  N = 88
Output : 7

方法:
有三种不同的情况:

  1. 如果N <10,则只能使用值1的硬币进行支付。
  2. N > 9且<25时,将使用值为1和10的硬币进行支付。在此,为了使所使用的硬币数量最少,将最优选值为10的硬币。
  3. N > 24时。然后将使用所有值1、10和25的硬币进行支付。为了最大程度地减少硬币的数量,主要目的是尽可能先使用价值25的硬币,然后再使用价值10的硬币,然后再使用价值1的硬币。

下面是上述方法的实现:

C++
// C++ program to find the minimum number
// of coins required
  
#include 
using namespace std;
  
// Function to find the minimum number
// of coins required
int countCoins(int n)
{
    int c = 0;
  
    if (n < 10) {
        // counts coins which have value 1
        // we will need n coins of value 1
        return n;
    }
    if (n > 9 && n < 25) {
        // counts coins which have value 1 and 10
        c = n / 10 + n % 10;
        return c;
    }
    if (n > 24) {
        // counts coins which have value 25
        c = n / 25;
  
        if (n % 25 < 10) {
  
            // counts coins which have value 1 and 25
            c = c + n % 25;
            return c;
        }
  
        if (n % 25 > 9) {
            // counts coins which have value 1, 10 and 25
            c = c + (n % 25) / 10 + (n % 25) % 10;
            return c;
        }
    }
}
  
// Driver Code
int main()
{
    int n = 14;
  
    cout << countCoins(n);
  
    return 0;
}


Java
// Java program to find the minimum number
// of coins required
  
class GFG {
  
    // Function to find the minimum number
    // of coins required
    static int countCoins(int n)
    {
        int c = 0;
        if (n < 10) {
            // counts coins which have value 1
            // we will need n coins of value 1
            return n;
        }
        if (n > 9 && n < 25) {
  
            // counts coins which have value 1 and 10
            c = n / 10 + n % 10;
  
            return c;
        }
        if (n > 24) {
            // counts coins which have value 25
            c = n / 25;
  
            if (n % 25 < 10) {
                // counts coins which have value 1 and 25
                c = c + n % 25;
  
                return c;
            }
            if (n % 25 > 9) {
                // counts coins which have value 1, 10 and 25
                c = c + (n % 25) / 10 + (n % 25) % 10;
  
                return c;
            }
        }
  
        return c;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 14;
        System.out.println(countCoins(n));
    }
}


Python3
# Python3 program to find the minimum number
# of coins required
  
# Function to find the minimum number
# of coins required
def countCoins(n):
  
    c = 0
  
    if (n < 10):
          
        # counts coins which have value 1
        # we will need n coins of value 1
        return n
      
    if (n > 9 and n < 25):
          
        # counts coins which have value 1 and 10
        c = n // 10 + n % 10
        return c
  
    if (n > 24):
          
        # counts coins which have value 25
        c = n // 25
  
        if (n % 25 < 10):
  
            # counts coins which have value
            # 1 and 25
            c = c + n % 25
            return c
  
        if (n % 25 > 9):
              
            # counts coins which have value
            # 1, 10 and 25
            c = (c + (n % 25) // 10 +
                     (n % 25) % 10)
            return c
  
# Driver Code
n = 14
  
print(countCoins(n))
  
# This code is contributed by mohit kumar


C#
// C# program to find the minimum number 
// of coins required 
using System;
  
class GFG
{ 
  
    // Function to find the minimum number 
    // of coins required 
    static int countCoins(int n) 
    { 
        int c = 0; 
        if (n < 10) 
        { 
            // counts coins which have value 1 
            // we will need n coins of value 1 
            return n; 
        } 
        if (n > 9 && n < 25)
        { 
  
            // counts coins which have value 1 and 10 
            c = n / 10 + n % 10; 
  
            return c; 
        } 
        if (n > 24)
        { 
            // counts coins which have value 25 
            c = n / 25; 
  
            if (n % 25 < 10) 
            { 
                // counts coins which have value 1 and 25 
                c = c + n % 25; 
  
                return c; 
            } 
            if (n % 25 > 9)
            { 
                // counts coins which have value 1, 10 and 25 
                c = c + (n % 25) / 10 + (n % 25) % 10; 
  
                return c; 
            } 
        } 
  
        return c; 
    } 
  
    // Driver Code 
    public static void Main() 
    { 
        int n = 14; 
        Console.WriteLine(countCoins(n)); 
    }
} 
  
// This code is contributed by Ryuga


PHP
 9 && $n < 25) 
    {
  
        // counts coins which have value 1 and 10
        $c = (int)($n / 10 + $n % 10);
  
        return $c;
    }
      
    if ($n > 24) 
    {
        // counts coins which have value 25
        $c = (int)($n / 25);
  
        if ($n % 25 < 10) 
        {
              
            // counts coins which have value 1 and 25
            $c = $c + $n % 25;
  
            return $c;
        }
        if ($n % 25 > 9)
        {
            // counts coins which have value 1, 10 and 25
            $c = $c + ($n % 25) / 10 + ($n % 25) % 10;
  
            return $c;
        }
    }
  
    return $c;
}
  
// Driver Code
$n = 14;
echo(countCoins($n));
  
// This code is contributed Code_Mech


输出:
5