1. #define null ""是用来将字符串清空的
  2. #define none -1是用来当不存在这种动物时,返回-1。

其实这种做法有点多余,不过好理解一些。

Home Web Board ProblemSet Standing Status Statistics
 

Problem J: 动物爱好者

Problem J: 动物爱好者

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

 

Append Code

[Submit][Status][Web Board]

  1. #include<iostream>
  2. #include<cstring>
  3. #define null ""
  4. #define none -1
  5. using namespace std;
  6. class Animal{
  7. public:
  8. string name;
  9. int num;
  10. Animal(string N=null,int n=):name(N),num(n){}
  11. void setAnimal(string N,int n){name=N;num=n;}
  12. int getNum() const{return num;}
  13. string getName() const{return name;}
  14. Animal &operator=(const Animal &a)
  15. {
  16. name=a.name;
  17. num=a.num;
  18. return *this;
  19. }
  20. };
  21. class AnimalList{
  22. friend class Animal;
  23. public:
  24. Animal *animalList;
  25. int numOfAnimal;
  26. AnimalList(Animal *animals, int n):animalList(animals),numOfAnimal(n){}
  27. int operator[](const string s)
  28. {
  29. for(int i=;i<numOfAnimal;i++)
  30. {
  31. if(animalList[i].name==s)
  32. return animalList[i].num;
  33.  
  34. }
  35. return none;
  36. }
  37. };
  38. int main()
  39. {
  40. int cases;
  41. string name;
  42. int num;
  43. cin>>cases;
  44. Animal animals[cases];
  45. for (int i = ; i < cases; i++)
  46. {
  47. cin>>name>>num;
  48. animals[i].setAnimal(name, num);
  49. }
  50. AnimalList animalList(animals, cases);
  51.  
  52. cin>>cases;
  53. for (int i = ; i < cases; i++)
  54. {
  55. cin>>name;
  56. if (animalList[name] != -)
  57. cout<<"There are "<<animalList[name]<<" "<<name<<"s."<<endl;
  58. else
  59. cout<<"There is none "<<name<<"."<<endl;
  60. }
  61. return ;
  62. }

实验12:Problem J: 动物爱好者的更多相关文章

  1. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal

    题目:Problem J. TerminalInput file: standard inputOutput file: standard inputTime limit: 2 secondsMemo ...

  2. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  3. The Ninth Hunan Collegiate Programming Contest (2013) Problem J

    Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...

  4. Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量

    Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...

  5. Problem J: 求个最大值

    Problem J: 求个最大值 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 871  Solved: 663[Submit][Status][Web ...

  6. Problem E: 动物爱好者

    Problem E: 动物爱好者 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 882  Solved: 699[Submit][Status][Web ...

  7. Problem J. Journey with Pigs

    Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...

  8. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem J. Joke 水题

    Problem J. Joke 题目连接: http://codeforces.com/gym/100714 Description The problem is to cut the largest ...

  9. 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...

随机推荐

  1. WebApp MVC 框架的开发细节归纳

    在前文<WebApp MVC,“不一样”的轻量级互联网应用程序开发框架>介绍了WebApp MVC的技术实现以及如何使用,而在本章进一步归纳了使用框架开发的一些细节,也给我们在开发具体功能 ...

  2. 2014-09-19.xml

    <wordbook><item>    <word>basel</word>     <trans><![CDATA[ n. 巴塞尔( ...

  3. Bill Gates说..

    世界不会在意你的自尊,人们看的只是你的成就.在你没有成就以前,切勿过分强调自尊.

  4. Linux下U盘变成只读

    今天用Ubuntu给同学拷贝数据的时候,突然其中一个文件夹U盘就不能复制和删除了.再windows7下可以删除除修改的那个文件夹之外的数据,但修改的那个文件夹死活删除不掉,只读属性也去不掉.再Ubun ...

  5. [转]VS2005/2008过期之后简单实用的升级方法

    网络上有不少key,但是用了之后没效果,发现了一个好方法可以解决.  把\vs\setup\下面的 setup.sdb文件用文本编辑器打开,然后改动其最后的一行([Product Key] 下面的一行 ...

  6. C#调用Java类

    C#调用Java类 (2011-01-07 14:02:05) 转载▼   分类: Java学习  1. 在Eclipse中新建名称为hello的java project,此工程仅包含一个文件hell ...

  7. Google Chrome Frame 自定义渲染方式,调用ActiveX

    通过meta段的设置可以控制浏览器的渲染行为,但在一些特殊情况下,meta段的设置无效,我们需要额外的操作以达到目的. 模式1:页面A(IE)iFrame引用页面B(Chrome Frame) 问题描 ...

  8. sqlserver -- 学习笔记(五)查询一天、一周、一个月记录(DateDiff 函数)(备忘)

    Learn From : http://bjtdeyx.iteye.com/blog/1447300 最常见的sql日期查询的语句 --查询当天日期在一周年的数据 ) --查询当天的所有数据 ) -- ...

  9. Linux - 进程状态

    ps report a snapshot of the current processes. 能提供一份当前进程的快照,以列表的形式显示正在运行的进程. 列出进程的数量取决于命令所附加的参数,例如:p ...

  10. mobile 更改hosts

    在Android下,/etc是link到/system/etc的,我们需要修改/system/etc/hosts来实现.但是这个文件是只读,不能通过shell直接修改.可以通过连接到PC上使用adb来 ...