Advertisement
Farjana_akter

Untitled

Jul 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int dp[100][100]={-1};
  4. int main()
  5. {
  6. freopen("in.txt","r",stdin);
  7. freopen("out.txt","w",stdout);
  8. int n,coin[100],i,j,k,value;
  9. cin>>value;
  10. cout<<"value is "<<value<<endl;
  11. cin>>n;
  12. for(i=1;i<=n;i++){
  13. cin>>coin[i];
  14. cout<<coin[i]<<" ";
  15. }
  16. cout<<endl;
  17. for(i=0;i<=n;i++)
  18. {
  19. for(j=0;j<=value;j++)
  20. {
  21. dp[i][j]=-1;
  22. }
  23. }
  24. for(i=0;i<=value;i++)
  25. dp[0][i]=0;
  26. for(i=0;i<=n;i++)
  27. dp[i][0]=0;
  28. for(i=0;i<=n;i++)
  29. {
  30. for(j=0;j<=value;j++)
  31. cout<<dp[i][j]<<" ";
  32. cout<<endl;
  33. }
  34. cout<<endl;
  35. cout<<endl;
  36. for(i=1;i<=n;i++)
  37. {
  38. cout<<coin[i]<<endl;
  39. for(j=1;j<=value;j++)
  40. {
  41. if(j<coin[i])
  42. {
  43. if(i-1>0)
  44. /*karon jkhn amra prothom row and colmn 0 kore dei thkn i jdi prothom row te thake tkhn i-1 holo
  45. // 0 tomo row.tai amra kkhno uporer value ta anbo na.aita sudu matro 1 no row er khtere projojjo
  46. // baki gular jnne dp[i-1][j] tomo gorer value ta e niya boshabe*/
  47. dp[i][j]=dp[i-1][j];
  48.  
  49. }
  50. else
  51. {
  52. if(i-1>0 && j-coin[i]>=0)
  53. // i-1 er reason upore deya ache. ar min value compare kore ber korbe uporer ta kom naki j-coin
  54. //tomo ghorer sathe 1 jog korle kom
  55. dp[i][j]=min(dp[i][j-coin[i]]+1,dp[i-1][j]);
  56. else if(j-coin[i]>=0){
  57. //jdi amr j-coin tomo ghor -1 na hoy tobe e amra 1 jog korbe otherwise -1 e print korbe
  58. if(dp[i][j-coin[i]]!=-1)
  59. dp[i][j]=dp[i][j-coin[i]]+1;
  60. }
  61. }
  62. }
  63. }
  64. for(i=0;i<=n;i++)
  65. {
  66. for(j=0;j<=value;j++)
  67. cout<<dp[i][j]<<" ";
  68. cout<<endl;
  69. }
  70. cout<<dp[n][value]<<endl;
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement