
Patrick B. answered 08/06/21
Math and computer tutor/teacher
// m is size of coins array (number of different coins)
int minCoins(int coins[], int m, int V)
{
// base case
if (V == 0) return 0;
// Initialize result
int res = INT_MAX;
// Try every coin that has smaller value than V
for (int i=0; i<m; i++)
{
if (coins[i] <= V)
{
int sub_res = minCoins(coins, m, V-coins[i]);
// Check for INT_MAX to avoid overflow and see if
// result can minimized
if (sub_res != INT_MAX && sub_res + 1 < res)
res = sub_res + 1;
}
}
return res;
}
// Driver program to test above function
int main()
{
QueType<int> q;
if (q.IsEmpty()) { cout<< "Queue is Empty"<<endl; } else { cout << "Queue is not empty"<<endl; }
if (q.IsFull()) { cout<< "Queue is Full"<<endl; } else { cout << "Queue is not full"<<endl; }
int iNums[]={5,7,4,2};
for (int iLoop=0; iLoop<4; iLoop++)
{
cout << "pushing " << iNums[iLoop] << endl;
q.Enqueue(iNums[iLoop]);
}
if (q.IsEmpty()) { cout<< "Queue is Empty"<<endl; } else { cout << "Queue is not empty"<<endl; }
if (q.IsFull()) { cout<< "Queue is Full"<<endl; } else { cout << "Queue is not full"<<endl; }
for (int iLoop=0; iLoop<4; iLoop++)
{
int iCurIntNum;
q.Dequeue(iCurIntNum);
cout << " popped "<<iCurIntNum << endl;
}
int numCoins=-1;
while (numCoins<1)
{
cout << "Please input # of coins :>";
cin>>numCoins;
}
int* coins = new int[numCoins];
for (int iLoop=0; iLoop<numCoins; iLoop++)
{
cout << "Please input coin value :>";
cin >> coins[iLoop];
}
int V = -1;
while (V<1)
{
cout <<"Please input the final amount :>";
cin >>V;
}
cout << "Minimum coins required is "
<< minCoins(coins, numCoins, V);
return 0;
}
Faria R.
the code is not running,Please check08/06/21