Charlie's Change(POJ-1787)

来源:互联网 发布:mac把原唱边伴奏 编辑:程序博客网 时间:2024/06/03 14:48

Description

Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task.

Your program will be given numbers and types of coins Charlie has and the coffee price. The coffee vending machines accept coins of values 1, 5, 10, and 25 cents. The program should output which coins Charlie has to use paying the coffee so that he uses as many coins as possible. Because Charlie really does not want any change back he wants to pay the price exactly.

Input

Each line of the input contains five integer numbers separated by a single space describing one situation to solve. The first integer on the line P, 1 <= P <= 10 000, is the coffee price in cents. Next four integers, C1, C2, C3, C4, 0 <= Ci <= 10 000, are the numbers of cents, nickels (5 cents), dimes (10 cents), and quarters (25 cents) in Charlie's valet. The last line of the input contains five zeros and no output should be generated for it.

Output

For each situation, your program should output one line containing the string "Throw in T1 cents, T2 nickels, T3 dimes, and T4 quarters.", where T1, T2, T3, T4 are the numbers of coins of appropriate values Charlie should use to pay the coffee while using as many coins as possible. In the case Charlie does not possess enough change to pay the price of the coffee exactly, your program should output "Charlie cannot buy coffee.".

Sample Input

12 5 3 1 216 0 0 0 10 0 0 0 0

Sample Output

Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters.Charlie cannot buy coffee.


题意:买咖啡,有1分,5分,10分,25分的硬币,怎么用最多的硬币买m分的咖啡;


ps:记录方案很好;


#include<iostream>#include<cstring>using namespace std;int m;int f[]={1,5,10,25};int a[4];int p[10005];int ans[10005];int dp[10005];int b[10005];int main(){while(cin>>m>>a[0]>>a[1]>>a[2]>>a[3]){if(m==0&&a[0]==0&&a[1]==0&&a[2]==0&&a[3]==0) break;memset(dp,-1,sizeof(dp));dp[0]=0;memset(p,0,sizeof(p));for(int i=0;i<4;i++){   //第几个memset(ans,0,sizeof(ans));   //每次都初始化一下数量for(int j=f[i];j<=m;j++){   //背包容量,正序,但是有限制if(dp[j]<dp[j-f[i]]+1&&dp[j-f[i]]>=0&&ans[j-f[i]]<a[i]){  //dp[j-f[i]]>=0是看能不能有j大小的包,ans[j-f[i]]<a[i]是用来限制数量的;dp[j]=dp[j-f[i]]+1;ans[j]=ans[j-f[i]]+1;   //数量记录p[j]=j-f[i];   //记录路径}}}memset(b,0,sizeof(b));if(dp[m]<0) cout<<"Charlie cannot buy coffee."<<endl;else{int i=m;while(1){   //读取路径if(i==0) break;b[i-p[i]]++;i=p[i];}cout<<"Throw in "<<b[1]<<" cents, "<<b[5]<<" nickels, "<<b[10]<<" dimes, and "<<b[25]<<" quarters."<<endl;}}return 0;}


原创粉丝点击