给定一个值 V,如果我们想改变 V Rs,我们有无限供应印度货币的每种面额,即我们有无限供应 { 1, 2, 5, 10, 20, 50, 100, 500, 1000} 面值的硬币/纸币,进行找零所需的最少硬币和/或纸币数量是多少?
例子:
Input: V = 70
Output: 2
We need a 50 Rs note and a 20 Rs note.
Input: V = 121
Output: 3
We need a 100 Rs note, a 20 Rs note and a 1 Rs coin.
解决方案:贪心法。
方法:一种常见的直觉是首先获取价值更高的硬币。这可以减少所需的硬币总数。从最大可能的面额开始,并在剩余值大于 0 时继续添加面额。
算法:
- 按降序对硬币数组进行排序。
- 将结果初始化为空。
- 找出小于当前金额的最大面额。
- 将找到的面额添加到结果中。从金额中减去找到的面额的值。
- 如果数量变为 0,则打印结果。
- 否则重复步骤 3 和 4 以获得新的 V 值。
C++
// C++ program to find minimum
// number of denominations
#include
using namespace std;
// All denominations of Indian Currency
int deno[] = { 1, 2, 5, 10, 20,
50, 100, 500, 1000 };
int n = sizeof(deno) / sizeof(deno[0]);
void findMin(int V)
{
sort(deno, deno + n);
// Initialize result
vector ans;
// Traverse through all denomination
for (int i = n - 1; i >= 0; i--) {
// Find denominations
while (V >= deno[i]) {
V -= deno[i];
ans.push_back(deno[i]);
}
}
// Print result
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << " ";
}
// Driver program
int main()
{
int n = 93;
cout << "Following is minimal"
<< " number of change for " << n
<< ": ";
findMin(n);
return 0;
}
C
// C program to find minimum
// number of denominations
#include
#define COINS 9
#define MAX 20
// All denominations of Indian Currency
int coins[COINS] = { 1, 2, 5, 10, 20,
50, 100, 200, 2000 };
void findMin(int cost)
{
int coinList[MAX] = { 0 };
int i, k = 0;
for (i = COINS - 1; i >= 0; i--) {
while (cost >= coins[i]) {
cost -= coins[i];
// Add coin in the list
coinList[k++] = coins[i];
}
}
for (i = 0; i < k; i++) {
// Print
printf("%d ", coinList[i]);
}
return;
}
int main(void)
{
// input value
int n = 93;
printf("Following is minimal number"
"of change for %d: ",
n);
findMin(n);
return 0;
}
// Code by Munish Bhardwaj
Java
// Java program to find minimum
// number of denominations
import java.util.Vector;
class GFG
{
// All denominations of Indian Currency
static int deno[] = {1, 2, 5, 10, 20,
50, 100, 500, 1000};
static int n = deno.length;
static void findMin(int V)
{
// Initialize result
Vector ans = new Vector<>();
// Traverse through all denomination
for (int i = n - 1; i >= 0; i--)
{
// Find denominations
while (V >= deno[i])
{
V -= deno[i];
ans.add(deno[i]);
}
}
// Print result
for (int i = 0; i < ans.size(); i++)
{
System.out.print(
" " + ans.elementAt(i));
}
}
// Driver code
public static void main(String[] args)
{
int n = 93;
System.out.print(
"Following is minimal number "
+"of change for " + n + ": ");
findMin(n);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find minimum
# number of denominations
def findMin(V):
# All denominations of Indian Currency
deno = [1, 2, 5, 10, 20, 50,
100, 500, 1000]
n = len(deno)
# Initialize Result
ans = []
# Traverse through all denomination
i = n - 1
while(i >= 0):
# Find denominations
while (V >= deno[i]):
V -= deno[i]
ans.append(deno[i])
i -= 1
# Print result
for i in range(len(ans)):
print(ans[i], end = " ")
# Driver Code
if __name__ == '__main__':
n = 93
print("Following is minimal number",
"of change for", n, ": ", end = "")
findMin(n)
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to find minimum
// number of denominations
using System;
using System.Collections.Generic;
class GFG{
// All denominations of Indian Currency
static int []deno = { 1, 2, 5, 10, 20,
50, 100, 500, 1000 };
static int n = deno.Length;
static void findMin(int V)
{
// Initialize result
List ans = new List();
// Traverse through all denomination
for(int i = n - 1; i >= 0; i--)
{
// Find denominations
while (V >= deno[i])
{
V -= deno[i];
ans.Add(deno[i]);
}
}
// Print result
for(int i = 0; i < ans.Count; i++)
{
Console.Write(" " + ans[i]);
}
}
// Driver code
public static void Main(String[] args)
{
int n = 93;
Console.Write("Following is minimal number " +
"of change for " + n + ": ");
findMin(n);
}
}
// This code is contributed by gauravrajput1
Javascript
输出:
Following is minimal number of change
for 93: 50 20 20 2 1
复杂度分析:
- 时间复杂度: O(V)。
- 辅助空间: O(V)。
注意:上述方法可能不适用于所有面额。例如,它不适用于面额 {9, 6, 5, 1} 和 V = 11。上述方法将打印 9, 1 和 1。但我们可以使用 2 个面额 5 和 6。
对于一般输入,可以使用以下动态规划方法:
找到产生给定值的最小硬币数量
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。