HDU 1497(Simple Library Management System)

模拟题,使用 set 数据结构存储每个用户当前借阅的所有图书。

#include 
#include 
#include 
#include 
using namespace std;
const int MAXM = 1005;
const int MAXN = 100005;

set user[MAXM]; //用户,每一个set包含该用户目前借阅的所有书
int book[MAXN]; //书的借阅状态,0表示在馆,1表示借出
map bookTOuser; //图书编号-用户编号

int main()
{
	int M, N;
	while (cin >> M >> N)
	{
		memset(book, 0, sizeof(book));
		bookTOuser.clear();
		for (int i = 1; i <= M; i++)
		{
			user[i].clear();
		}

		int C;
		cin >> C;
		char command; //命令
		int ui, bi; //用户编号,图书编号
		while (C--)
		{
			cin >> command;
			if (command == 'B') //借书
			{
				cin >> ui >> bi;
				if (book[bi] == 1)
					cout << "The book is not in the library now" << endl;
				else if (user[ui].size() == 9)
					cout << "You are not allowed to borrow any more" << endl;
				else
				{
					cout << "Borrow success" << endl;
					book[bi] = 1;
					user[ui].insert(bi);
					bookTOuser[bi] = ui;
				}
			}
			else if (command == 'R') //还书
			{
				cin >> bi;
				if (book[bi] == 0)
					cout << "The book is already in the library" << endl;
				else
				{
					cout << "Return success" << endl;
					book[bi] = 0;
					user[bookTOuser[bi]].erase(bi);
				}
			}
			else if (command == 'Q') //查询
			{
				cin >> ui;
				if (user[ui].empty())
					cout << "Empty" << endl;
				else
				{
					set::iterator it;
					it = user[ui].begin();
					cout << *it;
					for (++it; it != user[ui].end(); it++)
					{
						cout << " " << *it;
					}
					cout << endl;
				}
			}
		}
		cout << endl;
	}
	return 0;
}

继续加油。

你可能感兴趣的:(HDU)