zoukankan      html  css  js  c++  java
  • Problem D: 动物爱好者

     

    Problem D: 动物爱好者

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 1766  Solved: 1355
    [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。

    Append Code

    append.cc
    int main()
    {
        int cases;
        string name;
        int num;
        cin>>cases;
        Animal animals[cases];
        for (int i = 0; i < cases; i++)
        {
            cin>>name>>num;
            animals[i].setAnimal(name, num);
        }
        AnimalList animalList(animals, cases);
    
        cin>>cases;
        for (int i = 0; i < cases; i++)
        {
            cin>>name;
            if (animalList[name] != -1)
                cout<<"There are "<<animalList[name]<<" "<<name<<"s."<<endl;
            else
                cout<<"There is none "<<name<<"."<<endl;
        }
        return 0;
    }
    

      

    #include <iostream>
    using namespace std;
    class Animal
    {
    public :
        string name;
        int num;
        Animal():name("no name"), num(0){}
        void setAnimal(string arr,int n)
        {
            name=arr; num=n;
        }
        int getNum() const{return num;}
        string getName() const{return name;}
        Animal &operator=(Animal &p)
        {
            name=p.name; num=p.num;
        }
    };
    class AnimalList
    {
    public :
        Animal *animalList;
        int numOfAnimal;
        AnimalList(Animal *animals, int n):numOfAnimal(n)
        {
            animalList=new Animal[numOfAnimal];
            for(int i=0; i<n; i++)
            animalList[i]=animals[i];
        }
        int  operator[](string name_)
        {
            for(int i=0; i<numOfAnimal; i++)
            {
                if(animalList[i].name==name_)
                    return animalList[i].num;
            }
            return -1;
        }
        ~AnimalList(){delete []animalList;}
    };
    int main()
    {
        int cases;
        string name;
        int num;
        cin>>cases;
        Animal animals[cases];
        for (int i = 0; i < cases; i++)
        {
            cin>>name>>num;
            animals[i].setAnimal(name, num);
        }
        AnimalList animalList(animals, cases);
    
        cin>>cases;
        for (int i = 0; i < cases; i++)
        {
            cin>>name;
            if (animalList[name] != -1)
                cout<<"There are "<<animalList[name]<<" "<<name<<"s."<<endl;
            else
                cout<<"There is none "<<name<<"."<<endl;
        }
        return 0;
    }
    

      

    作者:7oDo

    仅供参考,请勿抄袭。

    Hang Hang Hang !!!

  • 相关阅读:
    54.施工方案第二季(最小生成树)
    53.FIB词链
    52.1076 排序
    最短路径:我的理解--SPFA算法
    POJ2187Beauty Contest
    CodeForces 279B Books
    SDUT 2527 斗地主
    HDU1020 Encoding
    POJ 2635 The Embarrassed Cryptographer
    POJ 1942 Paths on a Grid(组合数)
  • 原文地址:https://www.cnblogs.com/Jie-Fei/p/9128573.html
Copyright © 2011-2022 走看看