Advertisement
spider68

coin change problem min. coin

Mar 15th, 2020 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void test(int arr[],int n)
  5. {
  6.     int i,j;
  7.     int maxx=0;
  8.     int a[10][n+1];
  9.     for(i=0;i<10;i++)
  10.     {
  11.         for(j=0;j<=n;j++)a[i][j]=0;
  12.     }
  13.     for(i=0;i<10;i++)
  14.     {
  15.         for(j=1;j<=n;j++)
  16.         {
  17.             if(i==0)a[i][j]=j;
  18.             else{
  19.                 if(arr[i]>j)a[i][j]=a[i-1][j];
  20.                 else{
  21.                     a[i][j]=min(a[i-1][j],1+a[i][j-arr[i]]);
  22.                 }
  23.             }
  24.         }
  25.     }
  26.  
  27.     i=9;
  28.     j=n;
  29.     while(j!=0)
  30.     {
  31.         if(i>0&&a[i-1][j]==a[i][j])i--;
  32.         else{
  33.             cout<<arr[i]<<" ";
  34.             j-=arr[i];
  35.         }
  36.     }
  37. }
  38. int main()
  39. {
  40.     int i,j,m,n,t;
  41.     cin>>t;
  42.     int arr[] ={1,2,5,10,20,50,100,200,500,2000};
  43.     while(t--)
  44.     {
  45.         cin>>n;
  46.         test(arr,n);
  47.         printf("\n");
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement