#include<bits/stdc++.h>
#define N 10000
#define INF 9999999
using namespace std;
int c[50];
int num[50][N];
int n;
int cnt(int i, int w)
{
if(w < 0)
return INF;
if(i == n)
{
if(w == 0)
return 0;
return INF;
}
if(num[i][w] != -1)
return num[i][w];
int ans1 = 1 + cnt(i, w-c[i]);
int ans2 = cnt(i+1, w);
num[i][w] = min(ans1, ans2);
return num[i][w];
}
int main()
{
cout << "Enter Number of Coin :: ";
cin >> n;
cout << "Enter Coin :: ";
for(int i=0; i<n; i++)
cin >> c[i];
int target;
cout << "Enter Target Amount :: ";
cin >> target;
for(int i=0; i<=n; i++)
{
for(int j=0; j<=target; j++)
num[i][j] = -1;
}
int r = cnt(0, target);
if(r != INF)
cout << "Minimum Amount :: " << r << "\n";
else
cout << "Not Possible." << "\n";
return 0;
}
Comments
Post a Comment