I mean your borrowers of books — those mutilators of collections,
spoilers of the symmetryof shelves, and creators of odd volumes.
– (Charles Lamb, Essays of Elia (1823) ‘
The Two Races of Men’)Like Mr. Lamb, librarians have their problems with borrowers too. People don’t put books back wherethey should. Instead, returned books are kept at the main desk until a librarian is free to replace themin the right places on the shelves. Even for librarians, putting the right book in the right place canbe very time-consuming. But since many libraries are now computerized, you can write a program tohelp.
When a borrower takes out or returns a book, the computer keeps a record of the title. Periodically,the librarians will ask your program for a list of books that have been returned so the books can bereturned to their correct places on the shelves. Before they are returned to the shelves, the returnedbooks are sorted by author and then title using the ASCII collating sequence. Your program shouldoutput the list of returned books in the same order as they should appear on the shelves. For eachbook, your program should tell the librarian which book (including those previously shelved) is alreadyon the shelf before which the returned book should go.
Input
First, the stock of the library will be listed, one book per line, in no particular order. Initially, they areall on the shelves. No two books have the same title. The format of each line will be:title" by authorThe end of the stock listing will be marked by a line containing only the word:ENDFollowing the stock list will be a series of records of books borrowed and returned, and requestsfrom librarians for assistance in restocking the shelves. Each record will appear on a single line, in oneof the following formats:BORROW titleRETURN titleSHELVEThe list will be terminated by a line containing only the word:ENDOutputEach time the SHELVE command appears, your program should output a series of instructions for thelibrarian, one per line, in the format:Put title1 after title2or, for the special case of the book being the first in the collection:Put title firstAfter the set of instructions for each SHELVE, output a line containing only the word:ENDAssumptions & Limitations:1. A title is at most 80 characters long.2. An author is at most 80 characters long.3. A title will not contain the double quote (") character.
Sample Input
"The Canterbury Tales" by Chaucer, G.
"Algorithms" by Sedgewick, R."
The C Programming Language" by Kernighan, B. and Ritchie, D.
ENDBORROW "Algorithms"
BORROW "The C Programming Language"RETURN "Algorithms"
RETURN "The C Programming Language"
SHELVE
END
Sample OutputPut
"The C Programming Language" after "The Canterbury Tales"
Put "Algorithms" after "The C Programming Language"
END
题目,讲的就是图书的借,和归还的问题。
坑点:
(1)在原来书柜上面,第一位有书的时候,你们还的书,就要在他们之后,如果第一位没有书,你归还,那么你就是在第一位。
(2)每一次SHELVE,整理书籍之后,所有的书的数据,都要改为初始状态,改为都在图书馆上面的状态。记得每次都要输出END。
书的状态,分为三种,被借不还,被借还了,在原来的书柜上面。
我用-1,表示,在上面,0表示,借了未还,1表示借了还了。
SHELVE之后,放在书柜上面,我就把1和-1的状态,全改为-1.(在这里错了3次,3次WA,心态炸了啊。差点以为不能AC了。)
我下面的代码,虽然长了一点,但是主要是在存数据那部分。你简单的看一下注释,完全ok,不信的你可以试试。不要注意长度,看着注释,就可以了。毕竟思路一上来,代码数量,就控制不住了。请见谅啊。
代码如下:
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
int t;
struct node
{
char bookname[1010];
char name[1010];
int k;
} a[1010];
bool cmp(node n,node m)
{
if(strcmp(n.name,m.name)!=0)
{
if(strcmp(n.name,m.name)<0)
return true;
return false;
}
if(strcmp(n.bookname,m.bookname)<0)
return true;
return false;
}
///vis寻找之前是不是有书,没有动过
void find()
{
int vis=-1;
int biao = 0;
for(int i=0;i<t; i++)
{
if(a[i].k==-1)
{
vis=i;
}
if(a[i].k==1)
{
if(vis!=-1)
{
cout << "Put " << a[i].bookname << " after " << a[vis].bookname << endl;
}else{
cout << "Put " << a[i].bookname << " first" << endl;
}
///一定要改变,因为下一本书,要在这一本书之后。
vis = i;
}
}
cout<<"END"<<endl;
}
///初始化,全改为-1的状态
void fre()
{
for(int i=0;i<t;i++)
{
if(a[i].k!=0)
{
a[i].k=-1;
}
}
}
int main()
{
char str[105];
///初始化
memset(a,0,sizeof(a));
///读入书籍信息
while(gets(str))
{
if(strcmp(str,"END")==0)
break;
int q=0;
int bi=0;
int ni=0;
for(int i=0; i<strlen(str)-1; i++)
{
if(str[i]=='"' && i!=0)
{
a[t].bookname[bi++]=str[i];
q=1;
i=i+4;
continue;
}
if(q==0)
{
a[t].bookname[bi++]=str[i];
}
else
{
a[t].name[ni++]=str[i];
}
}
a[t].k=-1;
t++;
}
sort(a,a+t,cmp);
///开始对书籍进行操作
while(gets(str))
{
if(strcmp(str,"END")==0) break;
if(strcmp(str,"SHELVE")==0)
{
find();
fre();
continue;
}
char cz[105];
char book[105];
int q=0;
int ci=0;
int bi=0;
for(int i=0; i<strlen(str); i++)
{
if(str[i]!=' ' && q==0)
{
cz[ci++]=str[i];
}
if(str[i]==' ' && q==0)
{
q=1;
cz[ci]='\0';
continue;
}
if(q==1)
{
book[bi++]=str[i];
}
}
book[bi]='\0';
///还书
if(strcmp(cz,"BORROW")==0)
{
for(int i=0; i<t; i++)
{
if(strcmp(a[i].bookname,book)==0)
{
a[i].k=0;
break;
}
}
}
if(strcmp(cz,"RETURN")==0)
{
for(int i=0; i<t; i++)
{
if(strcmp(a[i].bookname,book)==0)
{
a[i].k=1;
break;
}
}
}
}
return 0;
}
题目,只要你思路清晰,代码明确,去分析一些可能的坑点,就可以了。