I need to give the minimum amount of change possible.I input number of cases,each case had a number of coins(1 is not necessarily a part of them) and the number of number I want to test.Then I enter the different coins and the different number to test.
I dont know why my program isnt working.Since 1 isnt necessarily part of the change,I had to tweak the program a little.
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<functional>
#include<numeric>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int n,i;
cin>>n;
int f=n,c,m;
int flag=0;
int m1;
int coins[100];
vector <int>storage(100,0);
vector <int> testcases(1000,0);
vector <int> answers(1000,-1);
while(n>0)
{
cin>>c;
cin>>m;
for(i=1;i<=c;i++)
{
cin>>coins[i];
}
for(i=1;i<=c;i++)
{
cin>>testcases[i];
}
m1=*max_element(testcases.begin(),testcases.end());
for(i=0;i<1000;i++)
{
answers[i]=-1;
}
i=0;
while(m1>=i)
{
i++;
flag=0;
for(int j=1;j<=c;j++)
{
if(i-coins[j]>=0)
{
storage[j]=answers[i-coins[j]];
flag=1;
}
else
storage[j]=-2;
}
if(flag==1)
{answers[i]=*min_element(begin(storage), end(storage),
[](int t1, int t2) {return t1 > 0 && (t2 <= 0 || t1 < t2);});
flag=0;
}
else
answers[i]=0;
}
if(m1==i)
{
for(int y=1;y<=m;y++)
{
cout<<answers[testcases[y]]<<endl;
}
}
}
return 0;
}
EDIT: By "not working" I mean its actually doesnt do anything.Its takes input and does nothing.I think it goes into an infinite loop.