1 //#include <bits/stdc++.h>2#include <cstdio>3#include <queue>4#include <algorithm>5#include <cstring>6#include <string>7#include <iostream>8 using namespacestd;9typedefLong Longll;Ten /** One Test Instructions: The shelve directive indicates that all returned but not yet listed books are sorted and inserted into the bookshelf in turn A Sorting method: first author from small to large, then the title from small to large - need to use some method of string class, if use C, need to use some methods of CString - Ideas: the Structure book save information, status indicates the state of the books, put into the vector - - The above idea looser, but because of lazy, the code will not change - ************** + : Added a vector to save the book information, in return and borrow books change status, need to traverse vector, mom ah too wasted resources - the right posture should be + Save the title with vector<string> title A map<string,book> Books K: Store author and status in the title V:book at So when you sort the vectors: vectors are sorted by the information in the map - bool Compare (string A, string b) { - if (Books[a].author = = Books[b].author) return a < b; - else return Books[a].author < Books[b].author; - } - Shelve operation Time in for (int i = 0; i < title.size (); i++) - if (Books[title[i]].status = = 0) { to Int J; + For (j = i; j >= 0;--j) - if (books[title[j]].status = = 1) break; the } * */ $ structbook{Panax Notoginseng stringtitle; - stringauthor; the intstatus; + }; A BOOLcmp (book B1,book B2) { the if(B1.author = =B2.author) + returnB1.title <B2.title; - returnB1.author <B2.author; $ } $Vector<book>Vec; - intMain () { - the stringStr,op,name,title,author; - while(Getline (cin,str)) {Wuyi if(str = ="END") Break; the intpos = str.find_last_of ("\""); -title = Str.substr (0, pos+1);//POS not included WuAuthor = str.substr (pos+5);//include Pos -Book TMP; Tmp.author = author; Tmp.title =title; AboutTmp.status =1; $ Vec.push_back (TMP); - } - sort (Vec.begin (), Vec.end (), CMP); - while(Cin >>op) { A if(OP = ="END") Break; + GetChar (); the if(OP = ="Borrow"){ - getline (cin,name); $ for(inti =0; I < vec.size (); i + +){ the if(Vec[i].title = =name) { theVec[i].status =-1; the Break; the } - } in}Else if(OP = ="RETURN"){ the getline (cin,name); the for(inti =0; I < vec.size (); i + +){ About if(Vec[i].title = =name) { theVec[i].status =0; the Break; the } + } -}Else if(OP = ="SHELVE"){ the for(inti =0; I < vec.size (); i + +){Bayi intIndex1 =0, Index2 =-1; the if(Vec[i].status = =0){ theIndex1 =i; - for(intj = index1-1; J >=0; J--){ - if(Vec[j].status = =1){ theIndex2 =J; the Break; the } the } - if(Index2 = =-1) cout <<"Put"<< Vec[index1].title <<" First"<<Endl; the Elsecout <<"Put"<< Vec[index1].title <<" After"<< Vec[index2].title <<Endl; theVec[index1].status =1; the }94 } thecout <<"END"<<Endl; the } the }98 return 0; About}
UVA borrowers (application of vectors and maps, simulation)