首页 > ACM题库 > HDU-杭电 > HDU 1497 Simple Library Management System-模拟-[解题报告] C++
2013
12-11

HDU 1497 Simple Library Management System-模拟-[解题报告] C++

Simple Library Management System

问题描述 :

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.

输入:

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.

输出:

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 .

样例输入:

5 10
9
R 1
B 1 5
B 1 2
Q 1
Q 2
R 5
Q 1
R 2
Q 1
5 10
9
R 1
B 1 5
B 1 2
Q 1
Q 2
R 5
Q 1
R 2
Q 1

样例输出:

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

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


Hint
Hint
Huge input, the function scanf() may work better than cin

题意:模拟图书馆借书

book用于记录 该书 被那个学生借走了

pos 用于记录这本书被借走的同学放在了第几位.

注意:每次还书 的时候要更新pos!!!!

#include<stdio.h>
 #include<string.h>
 #include<math.h>
 #include<queue>
 #include<stdlib.h>
 #include<algorithm>
 using namespace std;
 const int maxn = 100005;
 const int maxm = 1005;
 int book[ maxn ],pos[ maxn ];
 //int vis[ maxn ];
 struct node{
     int book[ 12 ];
     int cnt;
 }stu[ maxm ];
 int main(){
     int m,n;
     while( scanf("%d%d",&m,&n)!=EOF ){
         for( int i=0;i<=m;i++ ){
             stu[ i ].cnt=0;
         }
         memset( book,-1,sizeof( book ));//book[ i ] the i people
         memset( pos,-1,sizeof( pos ));// pos[ k ] the kth place
         int ca;
         scanf("%d",&ca);
         for( int t=1;t<=ca;t++ ){
             char tmp[5];
             scanf("%s",&tmp);
             if( tmp[0]=='B' ){
                 int u,b;
                 scanf("%d%d",&u,&b);
                 if( book[ b ]!=-1 ){
                     printf("The book is not in the library now\n");
                 }
                 else if( stu[ u ].cnt>=9 ){
                     printf("You are not allowed to borrow any more\n");
                 }
                 else {
                     stu[ u ].book[ stu[u].cnt ]=b;
                     pos[ b ]=stu[ u ].cnt;
                     book[ b ]=u;
                     stu[ u ].cnt++;
                     printf("Borrow success\n");
                 }
             }
             else if( tmp[0]=='R' ){
                 int b;
                 scanf("%d",&b);
                 if( book[ b ]==-1 ){
                     printf("The book is already in the library\n");
                 }
                 else {
                     printf("Return success\n");
                     for( int j=pos[b];j<stu[book[b]].cnt-1;j++ ){
                         stu[ book[b] ].book[ j ]=stu[ book[b] ].book[ j+1 ];
                         pos[ stu[ book[b] ].book[ j+1 ] ]=j;
                     }
                     
                     stu[ book[b] ].cnt--;
                     book[ b ]=-1;
                     pos[ b ]=-1;
                     
                 }
             }
             else if( tmp[0]=='Q' ){
                 int u;
                 scanf("%d",&u);
                 if( stu[ u ].cnt==0 ){
                     printf("Empty\n");
                 }
                 else {
                     int temp[ 12 ];
                     int k=0;
                     for( int i=0;;i++ ){
                         if( pos[stu[u].book[ i ]]!=-1 ){
                             temp[ k++ ]=stu[u].book[i];
                             if( k>=stu[u].cnt ) break;
                         }
                     }
                     sort( temp,temp+k );
                     for( int i=0;i<k;i++ ){
                         if( i==0 ) printf("%d",temp[i]);
                         else printf(" %d",temp[i]);
                     }
                     printf("\n");
                 }
             }
         }
         printf("\n");
     }
     return 0;
 }

 

解题报告转自:http://www.cnblogs.com/justforgl/archive/2013/02/17/2914078.html


  1. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。

  2. 为什么for循环找到的i一定是素数叻,而且约数定理说的是n=p1^a1*p2^a2*p3^a3*…*pk^ak,而你每次取余都用的是原来的m,也就是n

  3. 问题3是不是应该为1/4 .因为截取的三段,无论是否能组成三角形, x, y-x ,1-y,都应大于0,所以 x<y,基础应该是一个大三角形。小三角是大三角的 1/4.

  4. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥