首页 > 代码库 > POJ1787Charlie's Change
POJ1787Charlie's Change
Charlie‘s Change
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 2978 | Accepted: 844 |
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.
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.
Source
题意:给定一个总金额,然后分别是1,5,10,25每种硬币的数量,问最多可以拿多少硬币恰好支付
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>const int MX=10000;using namespace std;int main(){ int i,j,k,g[4],v,c[4]={1,5,10,25},dp[11000][5];//dp[.][0]表示最多的总硬币,dp[.][1~4]分别表示每种硬币的数量 while(1) { int s=0; scanf("%d",&v); s+=v; for(i=0;i<4;i++) { scanf("%d",g+i); s+=g[i]*c[i]; } //cout<<s<<endl; if(s==0)break; s-=v; if(s<v) { printf("Charlie cannot buy coffee.\n"); }else if(s==v){ printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",g[0],g[1],g[2],g[3]); }else{ memset(dp,-MX,sizeof dp); for(i=0;i<5;i++) dp[0][i]=0; for(i=0;i<4;i++) { k=1; while(g[i]-k>=0){ for(j=v;j>=c[i]*k;j--) { if(dp[j][0]<dp[j-c[i]*k][0]+k) { dp[j][0]=dp[j-c[i]*k][0]+k; for(int x=1;x<5;x++) dp[j][x]=dp[j-c[i]*k][x]; dp[j][i+1]+=k; } } g[i]-=k; k<<=1; } if(g[i]>0) { k=g[i]; for(j=v;j>=c[i]*k;j--) { if(dp[j][0]<dp[j-c[i]*k][0]+k) { dp[j][0]=dp[j-c[i]*k][0]+k; for(int x=1;x<5;x++) dp[j][x]=dp[j-c[i]*k][x]; dp[j][i+1]+=k; } } } } for(i=0;i<5;i++) { if(dp[v][i]<0) { printf("Charlie cannot buy coffee.\n");break; } } if(i==5){ printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",dp[v][1],dp[v][2],dp[v][3],dp[v][4]); } } } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。