标签:
12 5 3 1 2 16 0 0 0 1 0 0 0 0 0
Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters. Charlie cannot buy coffee.
1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 int val[5] = {1,5,10,25},cnt[5],dp[10010],path[10010],p; 5 int sel[10010]; 6 int main() { 7 while(~scanf("%d",&p)) { 8 bool flag = p; 9 for(int i = 0; i < 4; ++i) { 10 scanf("%d",cnt+i); 11 flag = flag || cnt[i]; 12 } 13 if(!flag) break; 14 memset(dp,-1,sizeof dp); 15 memset(path,0,sizeof path); 16 path[0] = -1; 17 for(int i = dp[0] = 0; i < 4; ++i){ 18 memset(sel,0,sizeof sel); 19 for(int j = val[i]; j <= p; ++j) 20 if(dp[j - val[i]] > -1 && dp[j - val[i]] >= dp[j] && sel[j - val[i]] < cnt[i]){ 21 sel[j] = sel[j - val[i]] + 1; 22 path[j] = j - val[i]; 23 dp[j] = dp[j-val[i]] + 1; 24 } 25 } 26 if(dp[p] < 0) printf("Charlie cannot buy coffee.\n"); 27 else{ 28 int ans[110] = {0}; 29 while(~path[p]){ 30 ans[p - path[p]]++; 31 p = path[p]; 32 } 33 printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",ans[val[0]],ans[val[1]],ans[val[2]],ans[val[3]]); 34 } 35 } 36 return 0; 37 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4425057.html