Problem D: 动物爱好者
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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;
}