1 Star 0 Fork 0

LLL2343 / vJudge

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
230.cpp 1.74 KB
一键复制 编辑 原始数据 按行查看 历史
lll2343 提交于 2022-03-06 23:34 . 230
#include<bits/stdc++.h>
using namespace std;
struct Book{
string title;
string author;
int state; // 0 表示在列 1表示借出 2表示归还但未排序
};
bool sortBook(Book& b1,Book& b2){
if(b1.author == b2.author){
return b1.title < b2.title;
}
return b1.author < b2.author;
}
Book GetBookInfo(string tmp){
Book b;
auto iter = tmp.find(" by ");
b.title = tmp.substr(1,iter-2);
b.author = tmp.substr(iter+4);
b.state = 0;
return b;
}
int FindBookByTitile(string btitle,vector<Book>& books){
int ix = 0;
while(ix<books.size() && books[ix].title!=btitle){
ix++;
}
if(ix == books.size())
return -1;
return ix;
}
void Shelve(vector<Book>& books){
string t1,t2="";
int ix =0;
while(ix<books.size()){
if(books[ix].state==0){
t2 = books[ix].title;
} else if(books[ix].state==2){
if(t2!=""){
cout<<"Put \""<<books[ix].title
<<"\" after \""<<t2<<"\"\n";
} else {
cout<<"Put \""<<books[ix].title
<< "\" first\n";
}
books[ix].state = 0;
t2 = books[ix].title;
}
ix++;
}
cout<<"END"<<endl;
}
int main(){
string tmp;
vector<Book> books;
while(getline(cin,tmp) && tmp!="END"){
Book b = GetBookInfo(tmp);
books.push_back(b);
}
sort(books.begin(),books.end(),sortBook);
while(getline(cin,tmp) && tmp != "END"){
char op = tmp[0];
if(op!='S'){
auto ti = tmp.find("\"");
string btitle = tmp.substr(ti+1,tmp.size()-1-ti-1);
int pos = FindBookByTitile(btitle,books);
if(op == 'B'){
// BORROW btitle
if(pos>=0 && pos<books.size()){
books[pos].state = 1;
}
} else if(op=='R'){
// RETURN
if(pos>=0 && pos<books.size()){
books[pos].state = 2;
}
}
} else {
// SHELVE
Shelve(books);
}
}
return 0;
}
1
https://gitee.com/lll2343/v-judge.git
git@gitee.com:lll2343/v-judge.git
lll2343
v-judge
vJudge
master

搜索帮助