Skip to content

Commit 8c3a2b3

Browse files
authored
Create Greedy_algorithm.cpp (#341)
1 parent ff278e4 commit 8c3a2b3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

CPP/Greedy_algorithm.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// All denominations of Indian Currency
5+
int deno[] = { 1, 2, 5, 10, 20, 50, 100, 500, 1000 };
6+
int n = sizeof(deno) / sizeof(deno[0]);
7+
8+
// Driver program
9+
void findMin(int V)
10+
{
11+
// Initialize result
12+
vector<int> ans;
13+
14+
// Traverse through all denomination
15+
for (int i = n - 1; i >= 0; i--) {
16+
// Find denominations
17+
while (V >= deno[i]) {
18+
V -= deno[i];
19+
ans.push_back(deno[i]);
20+
}
21+
}
22+
23+
// Print result
24+
for (int i = 0; i < ans.size(); i++)
25+
cout << ans[i] << " ";
26+
}
27+
28+
// Driver program
29+
int main()
30+
{
31+
int n = 93;
32+
cout << "Following is minimal number of change for " << n << " is ";
33+
findMin(n);
34+
return 0;
35+
}

0 commit comments

Comments
 (0)