hdu 1497 Simple Library Management System 【简单图书管理系统】

题目

After AC all the hardest problems in the world , the ACboy 8006 now has nothing to do . One day he goes to an old library to find a part-time job .It is also a big library which has N books and M users.The user’s id is from 1 to M , and the book id is from 1 to N . According to the library rules , every user are only allowed to borrow 9 books .But what surprised him is that there is no computer in the library , and everything is just recorded in paper ! How terrible , I must be crazy after working some weeks , he thinks .So he wants to change the situation .
In the other hand , after 8006’s fans know it , they all collect money and buy many computers for the library .
Besides the hardware , the library needs a management program . Though it is just a piece of cake for 8006 , the library turns to you , a excellent programer,for help .
What they need is just a simple library management program . It is just a console program , and have only three commands : Borrow the book , Return the book , Query the user .

1.The Borrow command has two parameters : The user id and the book id
The format is : “B ui bi” (1<=ui<=M , 1<=bi<=N)
The program must first check the book bi wether it’s in the library . If it is not , just print “The book is not in the library now” in a line .
If it is , then check the user ui .
If the user has borrowed 9 books already, print “You are not allowed to borrow any more” .
Else let the user ui borrow the book , and print “Borrow success”.
2.The Return command only has one parameter : The book id
The format is : “R bi” (1<=bi<=N)
The program must first check the book bi whether it’s in the library . If it is , just print “The book is already in the library” . Otherwise , you can return the book , and print “Return success”.
3.The Query command has one parameter : The user id
The format is : “Q ui” (1<=ui<=M)
If the number of books which the user ui has borrowed is 0 ,just print “Empty” , otherwise print the books’ id which he borrows in increasing order in a line.Seperate two books with a blank.

输入

The input file contains a series of test cases . Please process to the end of file . The first line contains two integers M and N ( 1<= M <= 1000 , 1<=N<=100000),the second line contains a integer C means the number of commands(1<=C<=10000). Then it comes C lines . Each line is a command which is described above.You can assum all the books are in the library at the beginning of each cases.

输出

For each command , print the message which described above .
Please output a blank line after each test.
If you still have some questions , see the Sample .

样例输入

5 10
9
R 1
B 1 5
B 1 2
Q 1
Q 2
R 5
Q 1
R 2
Q 1
5 10
9
R 1
B 1 5
B 1 2
Q 1
Q 2
R 5
Q 1
R 2
Q 1

标准输出

The book is already in the library
Borrow success
Borrow success
2 5
Empty
Return success
2
Return success
Empty

The book is already in the library
Borrow success
Borrow success
2 5
Empty
Return success
2
Return success
Empty
提示

Huge input, the function scanf() may work better than cin

题目分析

本题为通过普通编写程序来实现图书管理,需要大量的数来作为flag,此处采用book数组来描述书本的借入与借出情况,user数组描述借阅者的书目,cnt 数组描述借阅者借阅数目

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

int book[100005];
int n,m;
int user[1005][15];
int cnt[1005];//计数数组

void Borrow(int user_num,int number)//借出
{
	if(book[number]!=0)
	{
		printf("The book is not in the library now\n");
		return ;
	}
	if(cnt[user_num]==9)
	{
		printf("You are not allowed to borrow any more\n");
		return ;
	}
	int i;
	book[number] = user_num;
	user[user_num][cnt[user_num]]=number;
	++cnt[user_num];
	printf("Borrow success\n");
}

void Query(int user_num)//查阅
{
	if(cnt[user_num]==0)
	{
		printf("Empty\n");
		return ;
	}
	int i,sub[15];
	for(i=0;i<cnt[user_num];++i)
		sub[i]=user[user_num][i];
	sort(sub,sub+cnt[user_num]);//排序
	i=0;
	while(i<cnt[user_num])
	{
		if(i == 0) 
			cout <<sub[i++];
		else
			cout <<" "<<sub[i++];
	}
	printf("\n");
}

void Return(int number)//归还
{
	if(book[number]==0)
	{
		printf("The book is already in the library\n");
		return ;
	}
	for(int i = 0;i<cnt[book[number]];++i)
	{
		if(user[book[number]][i]==number)
		{
			user[book[number]][i]=user[book[number]][cnt[book[number]]-1];
			user[book[number]][cnt[book[number]]-1]=0;
			break;
		}
	}
	--cnt[book[number]];
	book[number]=0;
	printf("Return success\n");
}

void init()//初始化
{
	memset(book,0,sizeof(book));
	memset(user,0,sizeof(user));
	memset(cnt,0,sizeof(cnt));
}

int main()
{
	int T;
	char ch;
	int number,user_num;
	while(~scanf("%d %d",&m,&n))
	{
		init();
		scanf("%d",&T);
		while(T--)
		{
			scanf(" %c ",&ch);
			if(ch == 'R')//各操作对应的函数
			{
				scanf("%d",&number);
				Return(number);
			}
			else if(ch == 'B')
			{
				scanf("%d %d",&user_num,&number);
				Borrow(user_num,number);
			}
			else if(ch == 'Q')
			{
				scanf("%d",&user_num);
				Query(user_num);
			}
		}
		printf("\n");
	}
	return 0;
}

运算结果

在这里插入图片描述

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1497

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

registor11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值