Problem E: 动物爱好者
Problem E: 动物爱好者
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 882 Solved: 699
[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
注意:不能使用STL。
#include <iostream>
#include <string>
#include <cmath>
#define null ""
using namespace std;
class Animal
{
friend class AnimalList;
public:
string name;
int num;
Animal():name(s),num(n){}
void setAnimal(string s,int n){name=s;num=n;}
int getNum(){return num;}
string getName() const
{
return name;
}
Animal &operator =(const Animal &p)
{
name=p.name;
num=p.num;
return *this;
}
};
class AnimalList
{
friend class Animal;
public:
Animal *cats;
int numofcat;
AnimalList(Animal *s,int n):cats(s),numofcat(n){}
int operator [](const string s)
{
;i<numofcat;i++)
if(cats[i].name==s)
return cats[i].num;
;
}
};
int main()
{
int cases;
string name;
int num;
cin>>cases;
Animal animals[cases];
; i < cases; i++)
{
cin>>name>>num;
animals[i].setAnimal(name, num);
}
AnimalList animalList(animals, cases);
cin>>cases;
; i < cases; i++)
{
cin>>name;
)
cout<<"There are "<<animalList[name]<<" "<<name<<"s."<<endl;
else
cout<<"There is none "<<name<<"."<<endl;
}
;
}
Problem E: 动物爱好者的更多相关文章
- 实验12:Problem J: 动物爱好者
#define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web B ...
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
随机推荐
- JS组件系列——基于Bootstrap Ace模板的菜单Tab页效果优化
前言:之前发表过一篇 JS组件系列——基于Bootstrap Ace模板的菜单和Tab页效果分享(你值得拥有) ,收到很多园友的反馈,当然也包括很多诟病,因为上篇只是将功能实现了,很多细节都没有处理 ...
- Quartz学习——Spring和Quartz集成详解(三)
Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度.下面就对Spring集成Quartz进行简单的介绍和示例讲解!和上一节 Quar ...
- 【模板】AC自动机(加强版)
题目描述 有个由小写字母组成的模式串以及一个文本串.每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据. 每组数据的第一行为一 ...
- Hexo + GitHub Pages搭建博客
搭建 Node.js 环境 为什么要搭建 Node.js 环境? – 因为 Hexo 博客系统是基于 Node.js 编写的 Node.js 是一个基于 Chrome V8 引擎的 JavaScrip ...
- exp3.1实现顺序栈的各种操作
#include<iostream>using namespace std;#include<malloc.h>typedef char Elem;typedef struct ...
- Java可变参数以及一个简单应用
可变参数: Java1.5增加了新特性:可变参数:适用于参数个数不确定,类型确定的情况,java把可变参数当做数组处理. 注意:可变参数必须位于最后一项. 原因:当可变参数个数多余一个时,必将有一个不 ...
- 最美时光第三方UWP源码公开
自己大概写了一个星期的成果.. 使用了官方最美时光app的UI和图片资源,并没有调用官方接口 https://files.cnblogs.com/files/loyieking/NiceCountDo ...
- java四大会话技术
未经作者允许,不得转载 第一cookie技术 常用方法: new Cookie(),构造一个cookie getName() ,获取cookie的名字 getValue () ,取到具体cookie的 ...
- 入侵必练的CMD命令
入侵必练的CMD命令 我们都知道和目标主机建立IPC$连接后,要把后门,木马之类的软件传过去,其实这个命令是DOS基础的 命令,我这里就写个格式. 一.呵呵,命令一写就知道了吧,在网上看的太多了,其他 ...
- 本地连接 vmware服务器
在本机中装载虚拟机,安装redhat.需要调试使用redhat可以与Windows进行通讯. 分为多步,在此前提下,默认你已经安装好且可以vm 和虚拟机 1:点击虚拟机>设置>添加网络适配 ...