Minimum amount of change c++ -
i need give minimum amount of change possible.i input number of cases,each case had number of coins(1 not part of them) , number of number want test.then enter different coins , different number test.
i dont know why program isnt working.since 1 isnt part of change,i had tweak program 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: "not working" mean doesnt anything.its takes input , nothing.i think goes infinite loop.
universal solution: run app under debugger. step code, watch values of variables. compare values expect. try edit code, re-compile, , debug again. place breakpoints in problem places jump through code.
i see #include "stdafx.h"
, means using visual studio. here guide:
mastering debugging in visual studio 2010 - beginner's guide
Comments
Post a Comment