实验12:Problem J: 动物爱好者
- #define null ""是用来将字符串清空的
- #define none -1是用来当不存在这种动物时,返回-1。
其实这种做法有点多余,不过好理解一些。
Home | Web Board | ProblemSet | Standing | Status | Statistics |
Problem J: 动物爱好者
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 509 Solved: 376
[Submit][Status][Web Board]
Description
某人是一个狂热的动物爱好者,豢养了大量的各种动物。现在请定义两个类:
1. Animal类:
(1)string name和int num属性表示该种动物的名称和数量。
(2)无参构造函数。
(3)void setAnimal(string,int)方法,用于设置一个动物的相关属性。
(4)int getNum() const和string getName() const方法用于获得该动物的数量和名称。
(5)重载的赋值运算符=。
2. AnimalList类:
(1)Animal *animalList和int numOfAnimal属性,用于表示该人豢养的所有动物的列表以及动物的种类数。
(2)构造函数AnimalList(Animal *animals, int n)。
(3)重载的下标运算符[],int operator[](string name),用于返回参数name指定名称的动物的数量,当不存在这种动物时,返回-1。
Input
第一行M>0表示有M种动物,之后有M行,每行第一个字符串表示动物名称,第二个整数是该种动物的数量。
之后一个N>0表示有N个测试用的动物名称,之后又有N行,每行是一个动物名。
Output
输出共N行,格式见样例。
Sample Input
Dog 5
Bird 10
Cat 11
Duck 1
Sparrow 66
6
Dog
Bird
Cat
Duck
Sparrow
Bull
Sample Output
There are 10 Birds.
There are 11 Cats.
There are 1 Ducks.
There are 66 Sparrows.
There is none Bull.
HINT
Append Code
-->
한국어<中文فارسیEnglishไทยAll Copyright Reserved 2010-2011SDUSTOJTEAMGPL2.02003-2011HUSTOJ ProjectTEAM
Anything about the Problems, Please Contact Admin:admin
- #include<iostream>
- #include<cstring>
- #define null ""
- #define none -1
- using namespace std;
- class Animal{
- public:
- string name;
- int num;
- Animal(string N=null,int n=):name(N),num(n){}
- void setAnimal(string N,int n){name=N;num=n;}
- int getNum() const{return num;}
- string getName() const{return name;}
- Animal &operator=(const Animal &a)
- {
- name=a.name;
- num=a.num;
- return *this;
- }
- };
- class AnimalList{
- friend class Animal;
- public:
- Animal *animalList;
- int numOfAnimal;
- AnimalList(Animal *animals, int n):animalList(animals),numOfAnimal(n){}
- int operator[](const string s)
- {
- for(int i=;i<numOfAnimal;i++)
- {
- if(animalList[i].name==s)
- return animalList[i].num;
- }
- return none;
- }
- };
- int main()
- {
- int cases;
- string name;
- int num;
- cin>>cases;
- Animal animals[cases];
- for (int i = ; i < cases; i++)
- {
- cin>>name>>num;
- animals[i].setAnimal(name, num);
- }
- AnimalList animalList(animals, cases);
- cin>>cases;
- for (int i = ; i < cases; i++)
- {
- cin>>name;
- if (animalList[name] != -)
- cout<<"There are "<<animalList[name]<<" "<<name<<"s."<<endl;
- else
- cout<<"There is none "<<name<<"."<<endl;
- }
- return ;
- }
实验12:Problem J: 动物爱好者的更多相关文章
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
题目:Problem J. TerminalInput file: standard inputOutput file: standard inputTime limit: 2 secondsMemo ...
- Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset
Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem J
Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...
- Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量
Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...
- Problem J: 求个最大值
Problem J: 求个最大值 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 871 Solved: 663[Submit][Status][Web ...
- Problem E: 动物爱好者
Problem E: 动物爱好者 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 882 Solved: 699[Submit][Status][Web ...
- Problem J. Journey with Pigs
Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem J. Joke 水题
Problem J. Joke 题目连接: http://codeforces.com/gym/100714 Description The problem is to cut the largest ...
- 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...
随机推荐
- WebApp MVC 框架的开发细节归纳
在前文<WebApp MVC,“不一样”的轻量级互联网应用程序开发框架>介绍了WebApp MVC的技术实现以及如何使用,而在本章进一步归纳了使用框架开发的一些细节,也给我们在开发具体功能 ...
- 2014-09-19.xml
<wordbook><item> <word>basel</word> <trans><![CDATA[ n. 巴塞尔( ...
- Bill Gates说..
世界不会在意你的自尊,人们看的只是你的成就.在你没有成就以前,切勿过分强调自尊.
- Linux下U盘变成只读
今天用Ubuntu给同学拷贝数据的时候,突然其中一个文件夹U盘就不能复制和删除了.再windows7下可以删除除修改的那个文件夹之外的数据,但修改的那个文件夹死活删除不掉,只读属性也去不掉.再Ubun ...
- [转]VS2005/2008过期之后简单实用的升级方法
网络上有不少key,但是用了之后没效果,发现了一个好方法可以解决. 把\vs\setup\下面的 setup.sdb文件用文本编辑器打开,然后改动其最后的一行([Product Key] 下面的一行 ...
- C#调用Java类
C#调用Java类 (2011-01-07 14:02:05) 转载▼ 分类: Java学习 1. 在Eclipse中新建名称为hello的java project,此工程仅包含一个文件hell ...
- Google Chrome Frame 自定义渲染方式,调用ActiveX
通过meta段的设置可以控制浏览器的渲染行为,但在一些特殊情况下,meta段的设置无效,我们需要额外的操作以达到目的. 模式1:页面A(IE)iFrame引用页面B(Chrome Frame) 问题描 ...
- sqlserver -- 学习笔记(五)查询一天、一周、一个月记录(DateDiff 函数)(备忘)
Learn From : http://bjtdeyx.iteye.com/blog/1447300 最常见的sql日期查询的语句 --查询当天日期在一周年的数据 ) --查询当天的所有数据 ) -- ...
- Linux - 进程状态
ps report a snapshot of the current processes. 能提供一份当前进程的快照,以列表的形式显示正在运行的进程. 列出进程的数量取决于命令所附加的参数,例如:p ...
- mobile 更改hosts
在Android下,/etc是link到/system/etc的,我们需要修改/system/etc/hosts来实现.但是这个文件是只读,不能通过shell直接修改.可以通过连接到PC上使用adb来 ...