#include<bits/stdc++.h>
using namespace std;
bool compare(int A, int B)
{
return (A > B);
}
int coin(vector<int>cn, int n)
{
int i,j;
int t[n+1];
t[0] = 0;
for(i=1;i<=n;i++)
t[i] = INT_MAX;
for(i=1;i<=n;i++)
{
for(j=0;j<cn.size();j++)
{
if(cn[j] <= i)
{
int result = t[i-cn[j]];
if(result != INT_MAX && result + 1 < t[i])
t[i] = result + 1;
}
}
}
return t[n];
}
int main()
{
int n, i, tm;
cout << "Enter Coin Number :: ";
cin >> n;
vector<int>cn(n);
cout << "Enter Each Coin Amount :: " << "\n";
for(i=0;i<n;i++)
{
cin >> cn[i];
}
cout << "Enter Total Amount :: ";
cin >> tm;
sort(cn.begin(), cn.end(), compare);
cout << "Minimum Coin Required :: " << coin(cn, tm) << "\n";
return 0;
}
using namespace std;
bool compare(int A, int B)
{
return (A > B);
}
int coin(vector<int>cn, int n)
{
int i,j;
int t[n+1];
t[0] = 0;
for(i=1;i<=n;i++)
t[i] = INT_MAX;
for(i=1;i<=n;i++)
{
for(j=0;j<cn.size();j++)
{
if(cn[j] <= i)
{
int result = t[i-cn[j]];
if(result != INT_MAX && result + 1 < t[i])
t[i] = result + 1;
}
}
}
return t[n];
}
int main()
{
int n, i, tm;
cout << "Enter Coin Number :: ";
cin >> n;
vector<int>cn(n);
cout << "Enter Each Coin Amount :: " << "\n";
for(i=0;i<n;i++)
{
cin >> cn[i];
}
cout << "Enter Total Amount :: ";
cin >> tm;
sort(cn.begin(), cn.end(), compare);
cout << "Minimum Coin Required :: " << coin(cn, tm) << "\n";
return 0;
}
Comments
Post a Comment