热门标签 | 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


推荐阅读
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 本文讨论了Kotlin中扩展函数的一些惯用用法以及其合理性。作者认为在某些情况下,定义扩展函数没有意义,但官方的编码约定支持这种方式。文章还介绍了在类之外定义扩展函数的具体用法,并讨论了避免使用扩展函数的边缘情况。作者提出了对于扩展函数的合理性的质疑,并给出了自己的反驳。最后,文章强调了在编写Kotlin代码时可以自由地使用扩展函数的重要性。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 深入理解Kafka服务端请求队列中请求的处理
    本文深入分析了Kafka服务端请求队列中请求的处理过程,详细介绍了请求的封装和放入请求队列的过程,以及处理请求的线程池的创建和容量设置。通过场景分析、图示说明和源码分析,帮助读者更好地理解Kafka服务端的工作原理。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
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社区 版权所有