hdu 1497 Simple Library Manageme…

Simple Library Management System

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 756    Accepted Submission(s): 245


Problem Description
After AC all the hardest problems in the world , the ACboy 8006 now has nothing to do . One day he goes to an old library to find a part-time job .It is also a big library which has N books and M users.The user's id is from 1 to M , and the book id is from 1 to N . According to the library rules , every user are only allowed to borrow 9 books .But what surprised him is that there is no computer in the library , and everything is just recorded in paper ! How terrible , I must be crazy after working some weeks , he thinks .So he wants to change the situation .

In the other hand , after 8006's fans know it , they all collect money and buy many computers for the library .

Besides the hardware , the library needs a management program . Though it is just a piece of cake for 8006 , the library turns to you , a excellent programer,for help .

What they need is just a simple library management program . It is just a console program , and have only three commands : Borrow the book , Return the book , Query the user .

1.The Borrow command has two parameters : The user id and the book id
The format is : "B ui bi" (1<=ui<=M , 1<=bi<=N)
The program must first check the book bi wether it's in the library . If it is not , just print "The book is not in the library now" in a line . 
If it is , then check the user ui .
If the user has borrowed 9 books already, print "You are not allowed to borrow any more" .
Else let the user ui borrow the book , and print "Borrow success".

2.The Return command only has one parameter : The book id
The format is : "R bi" (1<=bi<=N)
The program must first check the book bi whether it's in the library . If it is , just print "The book is already in the library" . Otherwise , you can return the book , and print "Return success".

3.The Query command has one parameter : The user id
The format is : "Q ui" (1<=ui<=M)
If the number of books which the user ui has borrowed is 0 ,just print "Empty" , otherwise print the books' id which he borrows in increasing order in a line.Seperate two books with a blank.
 

Input
The input file contains a series of test cases . Please process to the end of file . The first line contains two integers M and N ( 1<= M <= 1000 , 1<=N<=100000),the second line contains a integer C means the number of commands(1<=C<=10000). Then it comes C lines . Each line is a command which is described above.You can assum all the books are in the library at the beginning of each cases.
 

Output
For each command , print the message which described above .
Please output a blank line after each test.
If you still have some questions , see the Sample .
 

Sample Input
 
    
5 10 
R 1 
B 1 5 
B 1 2 
Q 1 
Q 2 
R 5 
Q 1 
R 2 
Q 1 
5 10 
R 1 
B 1 5 
B 1 2 
Q 1 
Q 2 
R 5 
Q 1 
R 2 
Q 1
 

Sample Output
 
    
The book is already in the library 
Borrow success 
Borrow success 
2 5 
Empty 
Return success 
Return success 
Empty 

The book is already in the library 
Borrow success 
Borrow success 
2 5 
Empty 
Return success 
Return success 
Empty
#include
#include
#include
struct node
{
    int jb[10];
    int num;
}ui[1005];
int main()
{
    int M,N,n;
    int u,b,a[100010];
    char ch;
    while(scanf("%d%d",&M,&N)!=EOF)
    {
        
        memset(a,-1,sizeof(a));
        memset(ui,0,sizeof(ui));
        scanf("%d",&n);    
        while(n--)
        {
            getchar();
            scanf("%c",&ch);        
            if(ch=='B')
            {
            
                scanf("%d %d",&u,&b);
                if(a[b]!=-1)
                {
                    printf("The book is not in the library now\n");
                }
                else
                {
                    if(ui[u].num==9)
                    {
                        printf("You are not allowed to borrow any more\n");
                    }
                    else
                    {
                        printf("Borrow success\n");
                        ui[u].jb[ui[u].num]=b;
                        ui[u].num++;
                        a[b]=u;
                                            
                    }
                }
            }
            else if(ch=='R')
            {
                int j;
                scanf("%d",&b);
                if(a[b]==-1)
                {
                    printf("The book is already in the library\n");
                }
                else
                {
                    int temp;
                    int num;
                    num=ui[a[b]].num;
                    for(j=0;j
                    {
                        if(ui[a[b]].jb[j]==b)
                        {
                            temp=j;
                        }
                    }
                    for(j=temp;j
                    {
                        ui[a[b]].jb[j]=ui[a[b]].jb[j+1];
                    }
                    ui[a[b]].num--;
                    a[b]=-1;
                    printf("Return success\n");
                }
            }
            else if(ch=='Q')
            {
                scanf("%d",&u);
                if(ui[u].num == 0)
                {
                    printf("Empty\n");
                } 
                else 
                {
                    int num=ui[u].num;
                    for(int i=0;i
                    {
                        for(int j=num-1;j>i;--j)
                        {
                            if(ui[u].jb[i] > ui[u].jb[j])
                            {
                                int t = ui[u].jb[i];
                                ui[u].jb[i]=ui[u].jb[j];
                                ui[u].jb[j] = t; 
                            }
                        }
                    }
                    for(i=0;i
                    {
                        if( i!=0)
                            printf(" ");
                        printf("%d",ui[u].jb[i]);
                    }
                    printf("\n");
                }
            }
        
        }    
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(模拟)