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 ...
随机推荐
- Delphi中paramstr的用法
原型 function paramstr(i:index):string 对于任何application paramstr(0)都默认代表的是应用程序的 ...
- caffe源码 卷积层
通俗易懂理解卷积 图示理解神经网络的卷积 input: 3 * 5 * 5 (c * h * w) pading: 1 步长: 2 卷积核: 2 * 3 * 3 * 3 ( n * c * k * k ...
- 一.把传统服务做成dubbo分布式服务架构的步骤
1.把传统服务按照一定原则(根据项目的业务逻辑和场景)拆分成多个服务(主要服务是服务提供者和服务消费者,服务提供者或服务消费者的公共部分也可以拆分成其他服务,如公共DAO.公共工具类.公共实体,公共w ...
- js循环生成多个easyui datagrid数据网格时,初始化表格
$.each( content, function(i, item){ var info_tpl = "";var result_tpl = "";var pr ...
- PHP中 HTTP_HOST 和 SERVER_NAME 的区别
最近在开发站群软件,用到了根据访问域名判断子站点的相关问题.PHP获取当前域名有两个变量 HTTP_HOST 和 SERVER_NAME,我想知道两者的区别以及哪个更加可靠. 首先我想说,百度上那些转 ...
- OVS中arp响应的流表的实现
总结: 1.br-int 流表总体是按照Normal 的方式,即常规的交换机的转发方式进行转发.而br-tun 交换机则主要按照流表的方式进行转发. 2.一般情况下,VM发出的ARP请求,会在该VM的 ...
- 【原创】自用css reset
自己工作中常用的reset,和一些设置,实际用时会根据网站页面进行增删. /* Common style */html{ overflow-y:scroll; overflow-x:auto;}bod ...
- spring mvc 多线程并发
ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁地编写出优美的多线程程序. http://www.xuebuyuan.com/1628190.html 我们 ...
- win10 uwp 活动磁贴
本文翻译:https://mobileprogrammerblog.wordpress.com/2015/12/23/live-tiles-and-notifications-in-universal ...
- HTML form表单回车触发提交
<script type="text/javascript"> function submitByEnter() { if(event.key ...