Cash Machine

作者在 2010-04-16 15:51:06 发布以下内容

ZJU1366(Cash Machine)

主页  上一页  下一页

Cash Machine

Time limit: 10 Seconds   Memory limit: 32768K 

Total Submit: 245   Accepted Submit: 107 

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example,

N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10

means the machine has a supply of 10 bills of 100 each, 4 bills of 50 each, and 5 bills of 10 each.

Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine. The program input is from a text file. Each data set in the file stands for a particular transaction and has the format:

cash N n1 D1 n2 D2 ... nN DN

where 0 <= cash <= 100000 is the amount of cash requested, 0 <= N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k = 1, N. White spaces can occur freely between the numbers in the input. The input data are correct. For each set of data the program prints the result to the standard output on a separate line as shown in the examples below.

Sample Input

Sample Output

Comment

735 3 4 125 6 5 3 350

633 4 500 30 6 100 1 5 0 1

735 0

0 3 10 100 10 50 10 10

735

630

0

0

735=1* 350+3* 125+2* 5

630=6* 100+1* 30 or 21* 30

No cash delivered

No cash delivered

The first data set designates a transaction where the amount of cash requested is 735. The machine contains 3 bill denominations: 4 bills of 125, 6 bills of 5, and 3 bills of 350. The machine can deliver the exact amount of requested cash.

In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is 630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered cash.

In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is 0 and, therefore, the machine delivers no cash.

Problem Source: Southeast Europe 2002

 

/////对输入的数据 进行从大到小的排序 (运用了stdlib 中的qsort 函数),设置一个标志数组,记录的是所添加后的数据,以便后面直接取数进行加,

#include < stdio.h>

#include < string.h>

#include < stdlib.h>

struct Bill

{

int n ;

int D;

};

bool MASK[100001];

int cmp(const void *a , const void *b)         ///从大到小排序

{

Bill *A = (Bill *)a;

Bill *B = (Bill *)b;

return (B->D - A->D);

}

int main(int argc, char* argv[])

{

Bill myCash[11];

int N   ;

int cash ;

int max ;

int   i , j , k;

while(scanf("%d %d",&cash,&N ) !=EOF)

{

max = 0 ;

for(i = 0 ; i < N ; i++)

{

scanf("%d %d",&myCash[i].n,&myCash[i].D ) ;

}

if(cash == 0 ||N == 0 )

{

printf("0\n");

continue ;

}

memset(MASK ,false ,sizeof(MASK) );

qsort(myCash ,N ,sizeof(Bill) ,cmp);

int max =0;

MASK[0] = true ;

for( i= 0 ; i< N ;i++)

for( j =max; j>= 0;j--)

{

  if(MASK[j])

  {

   for( int k = 1 ;k<= myCash[i].n ;k++)

   {

    int t = j+ k*myCash[i].D ;

    if(t > cash )

     break;

    max = max > t ? max : t ;

    if(!MASK[t])

     MASK[t]= true ;

   }

  }

}

printf("%d\n",max );

}

return 0;

}

acm | 阅读 278 次
文章评论,共0条
游客请输入验证码
浏览278次
文章分类
文章归档
最新评论