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

5
Dog 5
Bird 10
Cat 11
Duck 1
Sparrow 66
6
Dog
Bird
Cat
Duck
Sparrow
Bull

Sample Output

There are 5 Dogs.
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: 动物爱好者的更多相关文章

  1. 实验12:Problem J: 动物爱好者

    #define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web B ...

  2. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  3. 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 ...

  4. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  5. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  6. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  7. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  8. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  9. [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 ...

随机推荐

  1. 第5章 不要让线程成为脱缰的野马(Keeping your Threads on Leash) ---线程优先权(Thread priority)

    有没有过这样的经验?你坐在你的车子里,目的地还在好几公里之遥,而时间已经很晚了.你拼命想告诉那些挡住你去路的人们,今天这个约会对你是多么多么重要,能不能请他们统统--呃--滚到马路外?很不幸,道路系统 ...

  2. 通过express搭建自己的服务器

    前言 为了模拟项目上线,我们就需要一个服务器去提供API给我们调用数据.这次我采用express框架去写API接口.所有请求都是通过ajax请求去请求服务器来返回数据.第一次用node写后端,基本就是 ...

  3. 怎样使用自定义标签简化 js、css 引入?

    国庆将至,工作兴致全无,来总结点项目里平时不起眼干货. 前端引入 js .css 一般是这样: <script type="text/javascript" src=&quo ...

  4. Jmeter的安装和启动时出现unable to access jarfile apachejmeter.jar error value=1错误处理

    Jmeter是纯Java开发的, 能够运行Java程序的系统一般都可以运行Jmeter, 如:Windows. Linux. mac等. 由于是由Java开发,所以自然需要jdk环境. Windows ...

  5. dos攻击命令

    net user //查看有哪些用户 net start //查看开启了哪些服务项目 net send ip "文本信息" //向对方发送消息(如果对方关了信使有可能会收不到) n ...

  6. wpf 画刷的分类

    System.Windows.Media.Brush最上一层画刷 System.Windows.Media.GradientBrush  线性画刷 ,下层主要有两种画刷 System.Windows. ...

  7. 初学者易上手的SSH-struts2 02Action获取表单数据-通配符

    在上一章中,我们已经搭建好了struts2的一个开发环境,那么这一章就来做一个简单的登录功能,以及介绍和使用struts2里面一个重要的东西-通配符. 第一步,在WebContent下面新建一个log ...

  8. 从开发到部署会用到的 Docker 命令

    本文的目的是理解容器开发在目标环境中部署的端到端流程,并列出这些操作所需的 Docker 命令.如果有任何Docker问题,请移步到最新最热的技术社区 IT帮 itbang.me 提问,短信通知专家来 ...

  9. JS实现时钟特效

    <!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"& ...

  10. 59、jQuery初识

    jQuery是由原生js写的所以说所有jQuery制作出来的效果都可以使用js做出来,jQuery出现的目的是为了优化代码,提高码代码的效率它将很多功能封装. 一.jQuery的认识 1.何为jque ...