poj 2818

    技术2022-05-20  30

     

    Making Change Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1125 Accepted: 627

    Description

    Grocery stores have long struggled with how to avoid long checkout lines that leave customers frustrated. The "10 item or less" express line has been a common technique, but many stores a now trying to do even better by featuring self-service checkout lines. Such a system needs to have a mechanism to give correct change to the customer at the end of the transaction.  Write a program that, given the amount of change in the machine, can determine the quantities of each type of coins to return to the customer while minimizing the total number of coins dispersed.

    Input

    Input consists of one or more lines, each of the form:  Q D N P C where Q is the number of quarters in the dispenser, D is the number of dimes, N the number of nickels, P the number of pennies, and C is the number of cents (0. . . 99) owed to the customer.  End of the input is signaled by a line of 5 zeros.

    Output

    For each line of input data, your program should output either:  Dispense # quarters, # dimes, # nickels, and # pennies.  or  Cannot dispense the desired amount.  if it is not possible to dispense the exact amount.

    Sample Input

    5 9 9 9 37 0 9 9 9 37 10 10 10 0 37 1 3 0 10 30 1 3 6 10 30 0 0 0 0 0

    Sample Output

    Dispense 1 quarters, 1 dimes, 0 nickels, and 2 pennies. Dispense 0 quarters, 3 dimes, 1 nickels, and 2 pennies. Cannot dispense the desired amount. Dispense 0 quarters, 3 dimes, 0 nickels, and 0 pennies. Dispense 1 quarters, 0 dimes, 1 nickels, and 0 pennies.

    Source

    Mid-Atlantic 2005, Practice Problem

    分析:好久没刷题了,这题。。。没啥好说的,因为我没看题,话说可以DFS,我用DP,为了帮测正确性。。。

    直接贴代码:

     

    #include<cstdio> int i,j,n,Q,D,N,P,C,a[140],f[100],p[100],ans[4]; inline void get(int &x,int p) { int i; x=x*p>C?(C/p):x; for(i=0;i<x;++i)a[n++]=p; } void count(int x) { if(x==0)return; if(x-p[x]==25)++ans[0]; if(x-p[x]==10)++ans[1]; if(x-p[x]==5)++ans[2]; if(x-p[x]==1)++ans[3]; count(p[x]); } int main(int argc, char* argv[]) { //freopen("car.in","r",stdin); //freopen("car.out","w",stdout); while(scanf("%d%d%d%d%d",&Q,&D,&N,&P,&C)!=EOF) { if(Q==0&&D==0&&N==0&&P==0&&C==0)break; n=0; get(Q,25); get(D,10); get(N,5); get(P,1); for(f[0]=0,i=1;i<=C;++i)f[i]=C+1; for(i=0;i<n;++i) for(j=C;j>=a[i];--j) if(f[j]>f[j-a[i]]+1) { f[j]=f[j-a[i]]+1; p[j]=j-a[i]; } if(f[C]>C)printf("Cannot dispense the desired amount./n"); else { for(i=0;i<4;++i)ans[i]=0; count(C); printf("Dispense %d quarters, %d dimes, %d nickels, and %d pennies./n",ans[0],ans[1],ans[2],ans[3]); } } return 0; } 

     


    最新回复(0)