oj1787Charlie's Change(多重背包+记录路径,每个包恰好被填满的基础上每个包的钱币的个数尽量多)

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 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.
意题:每一行为一组,第一个数为coffee的价格,后跟4个数,表示每种钱的数量。钱类型为1分,5分,10分,25分。用这些数量的钱去卖一个coffee,不能多也不能少。如果有多种可以卖的方案就输出用了钱币数量最多的一组方案,输出每种钱的数量。
解题:记录一下路径,具看代码。
#include<stdio.h>
struct nnn
{
    int tp,id,k,sumk;//分别代表当前钱的类型,前个点的位置,当前钱的类型的数量,钱总数量
}dp[10005];
int V;
void zeroonepack(int w,int tp,int k)
{
    for(int v=V;v>=w;v--)
    if(dp[v].sumk<dp[v-w].sumk+k&&dp[v-w].sumk)//如果dp[v-w].sumk!=0,说明包v-w 用了sumk个钱币恰好被填满
    {
        dp[v].sumk=dp[v-w].sumk+k;//填满用的总钱币数
        dp[v].id=v-w;//前一位置
        dp[v].k=k; //当前加的钱币数量
        dp[v].tp=tp;//当前加的钱币类型
    }
}
void complexepack(int w,int tp)
{
    for(int v=w;v<=V;v++)
    if(dp[v].sumk<dp[v-w].sumk+1&&dp[v-w].sumk)
    {
        dp[v].sumk=dp[v-w].sumk+1;
        dp[v].id=v-w;
        dp[v].k=1; dp[v].tp=tp;
    }
}
void mulitpack(int use,int n,int tp)
{
    if(use>V)return ;
    if(use*n>=V)
    complexepack(use,tp);
    else
    {
        int k=1;
        while(k<n)
        {
            zeroonepack(k*use,tp,k);
            n-=k; k*=2;
        }
        zeroonepack(n*use,tp,n);
    }
}
int main()
{
    int typ[6]={0,1,5,10,25},flog,k[6];
    while(1)
    {
        scanf("%d",&V); flog=0;
        for(int i=1;i<=4;i++)
        {
            scanf("%d",&k[i]); 
            flog+=k[i]*typ[i];
        }
        if(flog+V==0) break;
        if(flog<V)
        {
             printf("Charlie cannot buy coffee.\n");
        }
        else
        {
            for(int i=0;i<=V;i++)
            {
               dp[i].sumk=0;
               dp[i].id=-1;
            }
            dp[0].sumk=1;
            for(int i=1;i<=4;i++)
            {
                mulitpack(typ[i],k[i],i);
            }
            if(dp[V].sumk)
            {
                 for(int i=0;i<=4;i++)
                 k[i]=0;
                 int v=V;
                 while(v!=-1)//回溯路径
                 {
                     k[dp[v].tp]+=dp[v].k;
                     v=dp[v].id;
                 }
    printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",k[1],k[2],k[3],k[4]);
            }
            else
            printf("Charlie cannot buy coffee.\n");
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值