Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int dp[100][100]={-1};
- int main()
- {
- freopen("in.txt","r",stdin);
- freopen("out.txt","w",stdout);
- int n,coin[100],i,j,k,value;
- cin>>value;
- cout<<"value is "<<value<<endl;
- cin>>n;
- for(i=1;i<=n;i++){
- cin>>coin[i];
- cout<<coin[i]<<" ";
- }
- cout<<endl;
- for(i=0;i<=n;i++)
- {
- for(j=0;j<=value;j++)
- {
- dp[i][j]=-1;
- }
- }
- for(i=0;i<=value;i++)
- dp[0][i]=0;
- for(i=0;i<=n;i++)
- dp[i][0]=0;
- for(i=0;i<=n;i++)
- {
- for(j=0;j<=value;j++)
- cout<<dp[i][j]<<" ";
- cout<<endl;
- }
- cout<<endl;
- cout<<endl;
- for(i=1;i<=n;i++)
- {
- cout<<coin[i]<<endl;
- for(j=1;j<=value;j++)
- {
- if(j<coin[i])
- {
- if(i-1>0)
- /*karon jkhn amra prothom row and colmn 0 kore dei thkn i jdi prothom row te thake tkhn i-1 holo
- // 0 tomo row.tai amra kkhno uporer value ta anbo na.aita sudu matro 1 no row er khtere projojjo
- // baki gular jnne dp[i-1][j] tomo gorer value ta e niya boshabe*/
- dp[i][j]=dp[i-1][j];
- }
- else
- {
- if(i-1>0 && j-coin[i]>=0)
- // i-1 er reason upore deya ache. ar min value compare kore ber korbe uporer ta kom naki j-coin
- //tomo ghorer sathe 1 jog korle kom
- dp[i][j]=min(dp[i][j-coin[i]]+1,dp[i-1][j]);
- else if(j-coin[i]>=0){
- //jdi amr j-coin tomo ghor -1 na hoy tobe e amra 1 jog korbe otherwise -1 e print korbe
- if(dp[i][j-coin[i]]!=-1)
- dp[i][j]=dp[i][j-coin[i]]+1;
- }
- }
- }
- }
- for(i=0;i<=n;i++)
- {
- for(j=0;j<=value;j++)
- cout<<dp[i][j]<<" ";
- cout<<endl;
- }
- cout<<dp[n][value]<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement