1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
#include <iostream> #include <cstdio> #include <cstdlib> #include <iomanip> #include <fstream> #define INF 10000000 using namespace std; int availabe_coin[6]; int min_ways[1005]; int visit[6]; int coins[6]={5,10,20,50,100,200}; int cost; int cmp; void dp(int tcost,int index,int noOfsteps) { int tmp,v; if(tcost>=cost) { int d = tcost-cost; if(d>1003) return; if(min_ways[d]!=INF) { tmp = noOfsteps+min_ways[tcost-cost]; if(tmp<cmp) { cmp = tmp; return; } } } for(int i=index+1;i<6;i++) { if(availabe_coin[i]==0) continue; if(visit[i]==0) { visit[i]=1; v = availabe_coin[i]; for(int j=1;j<=v;j++) { availabe_coin[i]-=j; dp(tcost+j*coins[i],i,noOfsteps+j); availabe_coin[i]+=j; } visit[i]=0; } } } int main() { int total,v,p,q; for(int i=0;i<1005;i++) min_ways[i]=INF; min_ways[0]=0; for(int i=0;i<6;i++) { for(int j=coins[i];j<1005;j++) { min_ways[j]= min(min_ways[j],1+min_ways[j-coins[i]]); } } while(scanf("%d",&availabe_coin[0])) { visit[0]=0; total = availabe_coin[0]; for(int i=1;i<=5;i++) { scanf("%d",&availabe_coin[i]); visit[i]=0; total += availabe_coin[i]; } if(total==0) break; scanf("%d.%d",&p,&q); cost=q+p*100; cmp = INF; for(int i=0;i<6;i++) { if(availabe_coin[i]==0) continue; if(visit[i]==0) { visit[i]=1; v = availabe_coin[i]; for(int j=1;j<=v;j++) { availabe_coin[i]-=j; dp(0+j*coins[i],i,0+j); availabe_coin[i]+=j; } visit[i]=0; } } cout<<setw(3)<<cmp<<"\n"; } return 0; } |