标签:des os io strong for art ar div
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 2978 | Accepted: 844 |
Description
Input
Output
Sample Input
12 5 3 1 2 16 0 0 0 1 0 0 0 0 0
Sample Output
Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters. Charlie cannot buy coffee.
Source
#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;
}
POJ1787Charlie's Change,布布扣,bubuko.com
标签:des os io strong for art ar div
原文地址:http://blog.csdn.net/fljssj/article/details/38453247