zoukankan      html  css  js  c++  java
  • POJ

     POJ  1787  Charlie'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.

    题意:给你一个物品的价值 n 然后给你 4种硬币的数量(1 5 10 25这四种硬币) 问最多用多少硬币能正好凑够n 

    一眼看去就是多重背包还原路径这也是第一种解法
     1 //#include<bits/stdc++.h>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 using namespace std;
     7 #define inf 0x3f3f3f3f
     8 int vis[10001][90];
     9 int dp[10001];
    10 int q[4];
    11 int main()
    12 {   freopen("in.txt","r",stdin);
    13     ios::sync_with_stdio(false);
    14     int n,c[4];
    15     int v[4]={1,5,10,25};
    16     while(cin>>n>>c[0]>>c[1]>>c[2]>>c[3]&&(n+c[1]+c[2]+c[3]+c[0]))
    17     {
    18         int a[101];
    19         int b[101];
    20         int d[101];
    21         int cnt=0;
    22         for(int i=0;i<4;i++)
    23         {
    24             int j=1;
    25             while(j<=c[i])
    26             {
    27                     a[cnt]=j*v[i];
    28                     b[cnt]=j;
    29                     d[cnt]=i;
    30                     cnt++;
    31                     c[i]-=j;
    32                     j*=2;
    33             }
    34             if(c[i])
    35             {
    36                     a[cnt]=c[i]*v[i];
    37                     b[cnt]=c[i];
    38                     d[cnt]=i;
    39                     cnt++;
    40             }
    41 
    42 
    43         }
    44 
    45         dp[0]=1;
    46         for(int i=0;i<cnt;i++)
    47         {
    48 
    49             for(int j=n;j>=a[i];j--)
    50             {
    51                 if(dp[j-a[i]]&&dp[j]<dp[j-a[i]]+b[i])
    52                 {
    53                     dp[j]=dp[j-a[i]]+b[i];
    54                     vis[j][i]=1;
    55                 }
    56 
    57             }
    58 
    59         }
    60     //    cout<<dp[n]<<endl;
    61         if(!dp[n])
    62         {
    63             cout<<"Charlie cannot buy coffee."<<endl;
    64         }
    65         else
    66         {
    67             int j=n;
    68             for(int i=cnt-1;i>=0;i--)
    69             {
    70                 if(vis[j][i])
    71                 {  // cout<<d[i]<<endl;
    72                     q[d[i]]+=b[i];
    73                     j-=a[i];
    74                 }
    75 
    76             }
    77             cout<<"Throw in "<<q[0]<<" cents, "<<q[1]<<" nickels, "<<q[2]<<" dimes, and "<<q[3]<<" quarters."<<endl;
    78         }
    79         for(int i=0;i<=n;i++)
    80         {
    81             dp[i]=0;
    82             for(int j=0;j<cnt;j++)
    83             vis[i][j]=0;
    84         }
    85         q[0]=q[1]=q[2]=q[3]=0;
    86 
    87 
    88 
    89     }
    90 
    91 
    92 
    93     return 0;
    94 }
    View Code

     第二种完全背包 记录使用次数 大于时不更新

    写的有点乱,就这吧,这种方法时间短(自己还是想不起来..)

     1 //#include<bits/stdc++.h>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 using namespace std;
     7 #define inf 0x3f3f3f3f
     8 int dp[10001];
     9 int q[105];
    10 int fa[10001];
    11 int usr[10001];
    12 int main()
    13 {   //freopen("in.txt","r",stdin);
    14     ios::sync_with_stdio(false);
    15     int n,c[6];
    16     int v[9]={0,1,5,10,25};
    17     while(cin>>n>>c[1]>>c[2]>>c[3]>>c[4]&&(n+c[1]+c[2]+c[3]+c[4]))
    18     {
    19       memset(fa,0,sizeof(fa));
    20       memset(dp,0,sizeof(dp));
    21       memset(q,0,sizeof(q));
    22      dp[0]=1;
    23         for(int i=1;i<=4;i++)
    24         {
    25           memset(usr,0,sizeof(usr));
    26             for(int j=v[i];j<=n;j++)
    27             {
    28 
    29                 if(dp[j-v[i]]&&dp[j]<dp[j-v[i]]+1&&usr[j-v[i]]<c[i])
    30                 {
    31                     dp[j]=dp[j-v[i]]+1;
    32                     usr[j]=usr[j-v[i]]+1;
    33                     fa[j]=j-v[i];
    34                 }
    35 
    36             }
    37 
    38         }
    39         if(!dp[n])
    40         {
    41             cout<<"Charlie cannot buy coffee."<<endl;
    42         }
    43         else
    44         {
    45             while(n!=0)
    46             {
    47                 q[n-fa[n]]++;
    48                 n=fa[n];
    49 
    50             }
    51             cout<<"Throw in "<<q[1]<<" cents, "<<q[5]<<" nickels, "<<q[10]<<" dimes, and "<<q[25]<<" quarters."<<endl;
    52         }
    53 
    54         //q[0]=q[1]=q[2]=q[3]=0;
    55 
    56 
    57 
    58     }
    59 
    60 
    61 
    62     return 0;
    63 }
    View Code
    你要知道你什么都没有,你要靠自己。
  • 相关阅读:
    linux 开机启动设置
    Ubuntu安装Oracle时出现乱码,及其他安装错误
    ubuntu 设置DNS
    opencv cuda TK1 TX1 兼容设置
    int to string
    post文件的html
    vc 获得调用者的模块名称
    dlib landmark+人面识别
    android破解
    jquery $(document).ready() 与window.onload的区别
  • 原文地址:https://www.cnblogs.com/qq1976648487/p/8407199.html
Copyright © 2011-2022 走看看