POJ1787Charlie's Change

來源:互聯網
上載者:User

標籤:des   os   io   strong   for   art   ar   div   

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.

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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.