Advertisement
naeem043

C DP Coin Change in Details

Aug 10th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include<stdio.h>
  2. int c[5], amn = 50, dp[6][100],seq[10000];
  3.  
  4.  
  5.  
  6. int main()
  7. {
  8.     int j,i,coin,num,total_coin = 0,total_coin2=0,amount = 89;
  9.  
  10.  
  11.     c[0] = 5;
  12.     c[1] = 2;
  13.     c[2] = 10;
  14.     c[3] = 1;
  15.     c[4] = 20;
  16.  
  17.     for (i = 0; i < 6; i++)
  18.     {
  19.         for (j = 0; j < 6; j++)
  20.         {
  21.             if (c[j] < c[i])
  22.             {
  23.                 int tmp = c[i];
  24.                 c[i] = c[j];
  25.                 c[j] = tmp;
  26.             }
  27.         }
  28.     }
  29.  
  30.     for(i=0;i<6;i++)
  31.     {
  32.         for(j=0;j<2005;j++)
  33.         {
  34.             dp[i][j] = -1;
  35.         }
  36.     }
  37.     i = 0;
  38.  
  39.     while(amn>0)
  40.     {
  41.         if(c[i]<=amn)
  42.         {
  43.             coin = c[i];
  44.             num = amn/coin;
  45.             total_coin += num;
  46.             amn = amn - num*coin;
  47.             printf("Coin %d - %d times\n",c[i],num);
  48.         }
  49.         i++;
  50.     }
  51.  
  52.     printf("Total Coin Need: %d \n\n\n",total_coin);
  53.     i=0;
  54.     while(amount>0)
  55.     {
  56.         if(c[i]<=amount)
  57.         {
  58.             coin = c[i];
  59.             num = amount/coin;
  60.             total_coin2 += num;
  61.             amount = amount - num*coin;
  62.             printf("Coin %d - %d times\n",c[i],num);
  63.         }
  64.         i++;
  65.     }
  66.  
  67.     printf("Total Coin Need: %d \n",total_coin2);
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement