实验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 ...
随机推荐
- 对依赖倒置原则(DIP)及Ioc、DI、Ioc容器的一些理解
1.概述 所谓依赖倒置原则(Dependence Inversion Principle)就是要依赖于抽象,不要依赖于具体.简单的说就是要求对抽象进行编程,不要对实现进行编程,这样就降低了客户与实现模 ...
- Linux下U盘变成只读
今天用Ubuntu给同学拷贝数据的时候,突然其中一个文件夹U盘就不能复制和删除了.再windows7下可以删除除修改的那个文件夹之外的数据,但修改的那个文件夹死活删除不掉,只读属性也去不掉.再Ubun ...
- 【翻译】VSM 和触发器
原文:The VisualStateManager and Triggers Author:Carole Snyder Silverlight 推出了可视化状态管理器( Visual State Ma ...
- Maven进价:eclipse中集成maven
一.M2Eclipse插件 m2eclipse是一个在Eclipse中集成Maven的插件,有了该插件,用户可以方便的在Eclipse中执行Maven命令.创建Maven项目.修改POM文件等. 下载 ...
- 队列的图文解析 和 对应3种语言的实现(C/C++/Java)
概要 本章和介绍"栈"时的流程一样,先对队列进行介绍,然后分别给出队列的C.C++和Java三种语言的实现.内容包括:1. 队列的介绍2. 队列的C实现3. 队列的C++实现4. ...
- php动态获取函数参数
PHP 在用户自定义函数中支持可变数量的参数列表.其实很简单,只需使用 func_num_args() , func_get_arg() ,和 func_get_args() 函数即可. 可变参数并 ...
- 开放产品开发(OPD):产品负责人的工作原则和方法
月26日我将在2014 WOT全球软件技术峰会做相关的一个主题演讲[产品负责人的工作原则和方法],个原则和相应的一些方法. 以下是本次分享内容: 完整版如下,如果你喜欢想下载的话,点击 http:// ...
- 一起Polyfill系列:Function.prototype.bind的四个阶段
昨天边参考es5-shim边自己实现Function.prototype.bind,发现有不少以前忽视了的地方,这里就作为一个小总结吧. 一.Function.prototype.bind的作用 其实 ...
- angular设置title
Javascript框架在处理seo方面存在问题,因为爬虫在检索seo信息的时候会读不了js给其赋的值,导致搜索引擎收录不了或者收录了无效的信息,比如收录的可能是title={{title}}这样的, ...
- python下的orm基本操作(1)--Mysql下的CRUD简单操作(含源码DEMO)
最近逐渐打算将工作的环境转移到ubuntu下,突然发现对于我来说,这ubuntu对于我这种上上网,收收邮件,写写博客,写写程序的时实在是太合适了,除了刚接触的时候会不怎么完全适应命令行及各种权限管理, ...