#include<bits/stdc++.h>
using namespace std;
int coin_need(vector<int>cn, int tk)
{
int i, j, t[tk+1], s[tk+1];
t[0] = s[0] = 0;
for(i=1;i<=tk;i++)
{
int minimum = 1000000;
int coin = 0;
for(j=1;j<cn.size();j++)
{
if(cn[j] <= i)
{
if(1+t[i-cn[j]] < minimum)
{
minimum = 1 + t[i-cn[j]];
coin = j;
}
}
}
t[i] = minimum;
s[i] = coin;
}
cout << "\nMinimum Coin Required :: " << t[tk] << "\n\n";
cout << "Coins Are :: ";
int l = tk;
while(l > 0)
{
cout << cn[s[l]] << " ";
l = l - cn[s[l]];
}
cout << "\n";
}
int main()
{
int n, i, tk;
cout << "Enter Coin Number :: ";
cin >> n;
vector<int>v(n+1);
cout << "Enter Each Coin Value :: ";
for(i=1;i<=n;i++)
cin >> v[i];
sort(v.begin(), v.end());
cout << "Enter Total Amount :: ";
cin >> tk;
coin_need(v, tk);
return 0;
}
Comments
Post a Comment