Advertisement
niamul64

/////// coin change by matrix// un finished

Jul 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. /////// coin change by matrix// un finished
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define scInt(x) scanf("%d",&x)
  5. int dp[1000][1000];
  6. vector<int > coins;
  7. int start;
  8. int samne_jabo_koi_ghor;
  9. int coinChange(int am)
  10. {
  11.  
  12.  
  13.     int i,j;
  14.     if(dp[coins[coins.size()-1]][am])
  15.        return dp[coins[coins.size()-1]][am];
  16.     for(i=0; i<coins.size(); i++)
  17.     {
  18.         for(j=0; j<=am; j++)
  19.         {
  20.  
  21.             if(j==0)
  22.             {
  23.  
  24.                 dp[coins[i]][j]=1;
  25.                 printf("  dp[%d][%d]=%d \n",coins[i],j,dp[coins[i]][j]);
  26.                 continue;
  27.             }
  28.             else if(coins[i]==0)
  29.             {
  30.  
  31.                 dp[coins[i]][j]=0;
  32.                 printf("  dp[%d][%d]=%d \n",coins[i],j,dp[coins[i]][j]);
  33.                 continue;
  34.             }
  35.             if(coins[i]<j){
  36.             dp[coins[i]][j]= dp[coins[i-1]][j] ;
  37.             printf("  dp[%d][%d]=%d \n",coins[i],j,dp[coins[i-1]][j]);
  38.             continue;
  39.             }
  40.             else if(coins[i]==j){
  41.              dp[coins[i]][j]= dp[coins[i-1]][j]+1 ;
  42.                 samne_jabo_koi_ghor=j;
  43.                 printf("  dp[%d][%d]=%d \n",coins[i],j,dp[coins[i-1]][j]);
  44.             continue;
  45.             }
  46.             dp[coins[i]][j]= dp[coins[i-1]][j]+dp[coins[i]][j-samne_jabo_koi_ghor] ;
  47.  
  48.         }
  49.  
  50.     }
  51.  
  52.            return dp[coins[coins.size()-1]][am];
  53.  
  54. }
  55.  
  56.  
  57.  
  58. int main()
  59. {
  60.     start=0;
  61.  
  62.     int n,r,i,tem,amount;
  63.     puts ("enter number of coins the coins");
  64.    scInt(n);
  65.  
  66.         coins.push_back(0);
  67.         for(i=1; i<=n; i++)
  68.         {
  69.             scInt(tem);
  70.             coins.push_back(tem);
  71.         }
  72.         scInt(amount);
  73.  
  74.         printf("ways to have %d: %d\n",amount ,coinChange(amount));
  75.  
  76.         while(scInt(amount)!=EOF){
  77.              puts ("\n\nenter amount");
  78.              printf("ways to have %d: %d\n",amount ,coinChange(amount));
  79.  
  80.        }
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement