给定一个金额,找出总和等于给定金额的不同面额纸币的最小数量。从面额最高的纸币开始,尽量容纳给定金额的尽可能多的纸币。
我们可以假设我们有无限供应价值 {2000, 500, 200, 100, 50, 20, 10, 5, 1}
例子:
Input : 800
Output : Currency Count
500 : 1
200 : 1
100 : 1
Input : 2456
Output : Currency Count
2000 : 1
200 : 2
50 : 1
5 : 1
1 : 1
这个问题是硬币找零问题的一个简单变体。这里贪婪的方法工作,因为给定的系统是规范的(请参阅this和this了解详细信息)
以下是查找笔记数量的程序实现:
C++
// C++ program to accept an amount
// and count number of notes
#include
using namespace std;
// function to count and
// print currency notes
void countCurrency(int amount)
{
int notes[9] = { 2000, 500, 200, 100,
50, 20, 10, 5, 1 };
int noteCounter[9] = { 0 };
// count notes using Greedy approach
for (int i = 0; i < 9; i++) {
if (amount >= notes[i]) {
noteCounter[i] = amount / notes[i];
amount = amount - noteCounter[i] * notes[i];
}
}
// Print notes
cout << "Currency Count ->" << endl;
for (int i = 0; i < 9; i++) {
if (noteCounter[i] != 0) {
cout << notes[i] << " : "
<< noteCounter[i] << endl;
}
}
}
// Driver function
int main()
{
int amount = 868;
countCurrency(amount);
return 0;
}
Python3
# Python3 program to accept an amount
# and count number of notes
# Function to count and print
# currency notes
def countCurrency(amount):
notes = [2000, 500, 200, 100,
50, 20, 10, 5, 1]
noteCounter = [0, 0, 0, 0, 0,
0, 0, 0, 0]
print ("Currency Count -> ")
for i, j in zip(notes, noteCounter):
if amount >= i:
j = amount // i
amount = amount - j * i
print (i ," : ", j)
# Driver code
amount = 868
countCurrency(amount)
Java
// Java program to accept an amount
// and count number of notes
import java.util.*;
import java.lang.*;
public class GfG{
// function to count and
// print currency notes
public static void countCurrency(int amount)
{
int[] notes = new int[]{ 2000, 500, 200, 100, 50, 20, 10, 5, 1 };
int[] noteCounter = new int[9];
// count notes using Greedy approach
for (int i = 0; i < 9; i++) {
if (amount >= notes[i]) {
noteCounter[i] = amount / notes[i];
amount = amount - noteCounter[i] * notes[i];
}
}
// Print notes
System.out.println("Currency Count ->");
for (int i = 0; i < 9; i++) {
if (noteCounter[i] != 0) {
System.out.println(notes[i] + " : "
+ noteCounter[i]);
}
}
}
// driver function
public static void main(String argc[]){
int amount = 868;
countCurrency(amount);
}
/* This code is contributed by Sagar Shukla */
}
C#
// C# program to accept an amount
// and count number of notes
using System;
public class GfG{
// function to count and
// print currency notes
public static void countCurrency(int amount)
{
int[] notes = new int[]{ 2000, 500, 200, 100, 50, 20, 10, 5, 1 };
int[] noteCounter = new int[9];
// count notes using Greedy approach
for (int i = 0; i < 9; i++) {
if (amount >= notes[i]) {
noteCounter[i] = amount / notes[i];
amount = amount - noteCounter[i] * notes[i];
}
}
// Print notes
Console.WriteLine("Currency Count ->");
for (int i = 0; i < 9; i++) {
if (noteCounter[i] != 0) {
Console.WriteLine(notes[i] + " : "
+ noteCounter[i]);
}
}
}
// Driver function
public static void Main(){
int amount = 868;
countCurrency(amount);
}
}
/* This code is contributed by vt_m */
PHP
= $notes[$i])
{
$noteCounter[$i] = intval($amount /
$notes[$i]);
$amount = $amount -
$noteCounter[$i] *
$notes[$i];
}
}
// Print notes
echo ("Currency Count ->"."\n");
for ($i = 0; $i < 9; $i++)
{
if ($noteCounter[$i] != 0)
{
echo ($notes[$i] . " : " .
$noteCounter[$i] . "\n");
}
}
}
// Driver Code
$amount = 868;
countCurrency($amount);
// This code is contributed by
// Manish Shaw(manishshaw1)
?>
Javascript
输出:
Currency Count ->
500 : 1
200 : 1
100 : 1
50 : 1
10 : 1
5 : 1
1 : 3
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。