热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

hdu1497SimpleLibraryManagementSystem【简单图书管理系统】

题目AfterACallthehardestproblemsintheworld,theACboy8006nowhasnothingtodo.Onedayhegoestoanold

题目


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<&#61;ui<&#61;M , 1<&#61;bi<&#61;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<&#61;bi<&#61;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<&#61;ui<&#61;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<&#61; M <&#61; 1000 , 1<&#61;N<&#61;100000),the second line contains a integer C means the number of commands(1<&#61;C<&#61;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
EmptyThe 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


题目分析

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

代码

#include
#include
#include
#include
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]!&#61;0){printf("The book is not in the library now\n");return ;}if(cnt[user_num]&#61;&#61;9){printf("You are not allowed to borrow any more\n");return ;}int i;book[number] &#61; user_num;user[user_num][cnt[user_num]]&#61;number;&#43;&#43;cnt[user_num];printf("Borrow success\n");
}void Query(int user_num)//查阅
{if(cnt[user_num]&#61;&#61;0){printf("Empty\n");return ;}int i,sub[15];for(i&#61;0;i<cnt[user_num];&#43;&#43;i)sub[i]&#61;user[user_num][i];sort(sub,sub&#43;cnt[user_num]);//排序i&#61;0;while(i<cnt[user_num]){if(i &#61;&#61; 0) cout <<sub[i&#43;&#43;];elsecout <<" "<<sub[i&#43;&#43;];}printf("\n");
}void Return(int number)//归还
{if(book[number]&#61;&#61;0){printf("The book is already in the library\n");return ;}for(int i &#61; 0;i<cnt[book[number]];&#43;&#43;i){if(user[book[number]][i]&#61;&#61;number){user[book[number]][i]&#61;user[book[number]][cnt[book[number]]-1];user[book[number]][cnt[book[number]]-1]&#61;0;break;}}--cnt[book[number]];book[number]&#61;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 &#61;&#61; &#39;R&#39;)//各操作对应的函数{scanf("%d",&number);Return(number);}else if(ch &#61;&#61; &#39;B&#39;){scanf("%d %d",&user_num,&number);Borrow(user_num,number);}else if(ch &#61;&#61; &#39;Q&#39;){scanf("%d",&user_num);Query(user_num);}}printf("\n");}return 0;
}

运算结果

在这里插入图片描述

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid&#61;1497


推荐阅读
  • 深入理解Kafka服务端请求队列中请求的处理
    本文深入分析了Kafka服务端请求队列中请求的处理过程,详细介绍了请求的封装和放入请求队列的过程,以及处理请求的线程池的创建和容量设置。通过场景分析、图示说明和源码分析,帮助读者更好地理解Kafka服务端的工作原理。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • Week04面向对象设计与继承学习总结及作业要求
    本文总结了Week04面向对象设计与继承的重要知识点,包括对象、类、封装性、静态属性、静态方法、重载、继承和多态等。同时,还介绍了私有构造函数在类外部无法被调用、static不能访问非静态属性以及该类实例可以共享类里的static属性等内容。此外,还提到了作业要求,包括讲述一个在网上商城购物或在班级博客进行学习的故事,并使用Markdown的加粗标记和语句块标记标注关键名词和动词。最后,还提到了参考资料中关于UML类图如何绘制的范例。 ... [详细]
  • 本文介绍了使用哈夫曼树实现文件压缩和解压的方法。首先对数据结构课程设计中的代码进行了分析,包括使用时间调用、常量定义和统计文件中各个字符时相关的结构体。然后讨论了哈夫曼树的实现原理和算法。最后介绍了文件压缩和解压的具体步骤,包括字符统计、构建哈夫曼树、生成编码表、编码和解码过程。通过实例演示了文件压缩和解压的效果。本文的内容对于理解哈夫曼树的实现原理和应用具有一定的参考价值。 ... [详细]
  • 本文介绍了一个React Native新手在尝试将数据发布到服务器时遇到的问题,以及他的React Native代码和服务器端代码。他使用fetch方法将数据发送到服务器,但无法在服务器端读取/获取发布的数据。 ... [详细]
  • dotNet变量和数据类型详解,包括声明变量和五大类型
    本文详细介绍了dotNet编程中的变量和数据类型,包括声明变量和五大类型(int、double、decimal、string、char)。文章通过案例演示了变量的声明和赋值方法,并解释了每种数据类型的特点和使用场景。此外,还介绍了变量命名规则和一些特殊情况,如String与string的区别、float类型的使用等。阅读本文可以帮助读者更好地理解和应用dotNet编程中的变量和数据类型。 ... [详细]
  • 使用eclipse创建一个Java项目的步骤
    本文介绍了使用eclipse创建一个Java项目的步骤,包括启动eclipse、选择New Project命令、在对话框中输入项目名称等。同时还介绍了Java Settings对话框中的一些选项,以及如何修改Java程序的输出目录。 ... [详细]
author-avatar
有些疯癫的小红帽
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有