uva 230

原题

也是纯模拟题, 远没有上次高尔夫球赛问题复杂..

就构造一个Book结构, 把涉及的变量都加进去, 除了title, author还有一个state

注意有三种state, (一开始以为borrow指令是无意义的, 其实还是有影响的, 借出去的书遍历的时候需要直接跳过了)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring> 
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <deque> 
#include <map>
#include <cassert>
#include <iomanip>
#include <list>
using namespace std;
const int MAXN = 170;
typedef long long LL;


/*
uva 230
*/

const int notBorrowed = 0;
const int isBorrowed  = 1;
const int isReturned  = 2;

struct Book{
	string title;
	string author;
	int state;
	Book(){ state = notBorrowed; }
	Book(string _title, string _author){
		title  = _title;
		author = _author;
		state  = notBorrowed;
	}

};

bool operator<(const Book & b1,const Book & b2){
	if( b1.author!=b2.author ){
		return b1.author < b2.author;
	}
	return b1.title < b2.title;
}



vector<Book> shelve;
string str;
char input[MAXN];



Book & Find(const string & title){
	int i;
	for(i=0; i<shelve.size(); i++){
		if( shelve[i].title==title ){
			break;
		}
	}
	return shelve[i];
}

int main(){
//	freopen("input2.txt","r",stdin);
	string title, cmd,author;
	shelve.clear();
	
	while( cin>>input[0] && input[0]!='E' ){
		fgets(input+1,MAXN,stdin);
		input[strlen(input)-1] = '\0';
		char * ptr = strstr(input," by ");
		ptr[0] = '\0';						
		title = input;
		author = (ptr+4);
		shelve.push_back(Book(title,author));
	}
	
	while( cin>>cmd && cmd!="END" ){
		if( cmd=="BORROW" ){
			getchar();
			getline(cin,title);
			Find(title).state = isBorrowed;
		}else if( cmd=="RETURN" ){
			getchar();
			getline(cin,title);
			Find(title).state = isReturned;
		}else if( cmd=="SHELVE" ){
			int lastBook = -1;
			sort(shelve.begin(),shelve.end(),less<Book>());
			for(int i=0; i<shelve.size(); i++){
//				cout << shelve[i].title << "---" << shelve[i].author << " : " << shelve[i].state << endl;
				if( shelve[i].state==notBorrowed ){
					lastBook = i;
				}else if( shelve[i].state==isBorrowed ){
					continue;
				}else if( shelve[i].state==isReturned ){
					shelve[i].state = notBorrowed;
					if( lastBook<0 ){
						// put first
						cout << "Put " << shelve[i].title << " first" << endl;
					}else{
						// put after a book
						cout << "Put " << shelve[i].title << " after " << shelve[lastBook].title << endl;
					}
					lastBook = i;
				}
			}
			cout << "END" << endl;
		}
	}
	return 0;
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值