实验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 ...
随机推荐
- CG
//设置线的宽度 CGContextSetLineWidth(ctx, 12); //设置线的连接处 (拐点) CGContextSetLineJoin(ctx, kCGLineJoinRound ) ...
- Sidekiq任务堵塞问题
今天发现服务器上的sidekiq 线程全忙.队列里任务积累了好多. sidekiq里的任务基本都是爬虫脚本,都需要请求外部网站.但是有些网站的响应时间或者读取时间太久,一直一直卡在那里.使得后面的任务 ...
- FLEX自定义事件
有时候我们需要让两个组件之间实现联动,并且在其中传递数据,自定义事件机制可以帮助我们比较优雅的实现这种需要. 下面的例子,是打算实现一个列表和一个编辑框的联动. 编辑框代码 <?xml vers ...
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问
本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...
- Web离线存储的几种方式
随着HTML5的正式定稿,我们也可以大量使用HTML离线网络应用程序的特性. #1.Application Cache Application Cache 可以很简单让我们的WebApp具有离线的能力 ...
- gulp-clean----gulp系列(五)
前面说过,当css,img,js出现删除操作的时候,虽然watch会监听,但是并不会删除相应文件. 现在实现clean任务,执行任务前先删除一次build目录. 先配置JS任务,设置删除目录. 在系列 ...
- [Logstash]使用详解
Logstash是一款轻量级的日志搜集处理框架,可以方便的把分散的.多样化的日志搜集起来,并进行自定义的处理,然后传输到指定的位置,比如某个服务器或者文件. 本文针对官方文档进行翻译以及实践,希望有更 ...
- .net 根据匿名类生成实体类,根据datatable生成实体类,根据sql生成实体类
在开发中可能会遇到这几种情况 1.EF或LINQ查询出来的匿名对象在其它地方调用不方便,又懒的手动建实体类 2.通过datatable反射实体需要先建一个类 ,头痛 3.通过SQL语句返回的实体也需要 ...
- MyBatis魔法堂:ResultMap详解
一.前言 MyBatis是基于“数据库结构不可控”的思想建立的,也就是我们希望数据库遵循第三范式或BCNF,但实际事与愿违,那么结果集映射就是MyBatis为我们提供这种理想与现实间转换的手段了, ...
- Gradle学习系列之五——自定义Property
在本系列的上篇文章中,我们讲到了增量式构建,在本篇文章中,我们将讲到如何自定义Project的Property. 请通过以下方式下载本系列文章的Github示例代码: git clone https: ...