首页 > 分享 > Problem D: 动物爱好者

Problem D: 动物爱好者

最新推荐文章于 2023-10-03 20:38:43 发布

小曹曹12138 于 2018-05-27 19:37:31 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

Problem D: 动物爱好者

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 1764   Solved: 1354
[ 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

Sample Input

5Dog 5Bird 10Cat 11Duck 1Sparrow 666DogBirdCatDuckSparrowBull

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

Append Code

[ Submit][ Status][ Web Board]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

#include <bits/stdc++.h>

#define null ""

#define none -1

using namespace std;

class Animal{

public :

     string name;

     int  num;

     Animal(string N=null, int n=0):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=0;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 = 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;

}

相关知识

Problem D: 动物爱好者
动物肖像d
某市体委对该市业余体育运动爱好者的一项调查显示:所有的桥牌爱好者都爱好围棋,有围棋爱好者爱好武术;所有的武术爱好者都不爱好健身操,有桥牌爱好者同时爱好健身操。如
动物爱好者的烦恼
【01】动物白内障手术D
【维生素d】维生素d的作用
维生素D
维生素D宠物用药查询/兽药维生素D使用说明
D伯爵
===动态规划===

网址: Problem D: 动物爱好者 https://m.mcbbbk.com/newsview620920.html

所属分类:萌宠日常
上一篇: 神奇宠物在这里!
下一篇: 养宠物蛇是一种怎样的体验,有什么