1、C +考试01背包问题#include #include#includeint min(int w,int c)int temp; if (wc) temp=w; else temp=c; return temp;void knapsack(int v,int w,int c,int n,int*m) /求最优值 int jmax=min(wn-1,c); for(int j=0;j=jmax;j+) mnj=0; for(int jj=wn;jj1;i-) /递归部分 jmax=min(wi-1,c); for(int j=0;j=jmax;j+) mij=mi+1j; for(int jj
2、=wi;jj=w1) m1c=max(m1c,m2c-w1+v1); cout最优值:m1cendl; for(int l=2;l=n;l+) for(int j=0;j=c;j+) coutmljsetw(c-1); coutendl; cout*endl;int traceback(int *m,int w,int c,int n,int x) /回代,求最优解 cout得到的一组最优解如下:endl; for(int i=1;in;i+) if(mic=mi+1c) xi=0; else xi=1; c-=wi; xn=(mnc)?1:0; for(int y=1;y=n;y+) cou
3、tsetw(5)xy; return xn; void main() int n,c; int *m; cout&欢迎使用0-1背包问题程序&endl; coutnc; int *v=new intn+1; coutPls input the property (vi):endl; for(int i=1;ivi; int *w=new intn+1; coutPls input the weight (wi):endl; for(int j=1;jwj; int *x=new intn+1; m=new int*n+1; /动态的分配二维数组 for(int p=0;pn+1;p+) mp=
4、new intc+1; knapsack(v,w,c,n,m); traceback(m,w,c,n,x);循环赛制#include#include#define TEAMCOUNT 4using namespace std;class team /定义一个球队类 public: team():paly_amount(0), win_amount(0), tie_amount(0), lose_amount(0), in_amount(0), out_amount(0), accumulate_points(0) coutInput the name of the +team_count te
5、am:team_name; team(int) void change(int mine, int opponent); friend void input_score(); friend void sort(); friend void display();private: string team_name; /队名 int paly_amount; /比赛场数 int win_amount; /赢的场数 int tie_amount; /平的场数 int lose_amount; /输的场数 int in_amount; /进球数 int out_amount; /失球数 int accu
6、mulate_points; /总积分 static int team_count; /球队数; int team:team_count = 0; team tmTEAMCOUNT; /球队数组 void team:change(int mine, int opponent) paly_amount+; in_amount+=mine; lose_amount+=opponent; if(mine opponent) win_amount+; accumulate_points+=3; else if(mine opponent) lose_amount+;else tie_amount+;
7、accumulate_points+=1;void input_score()string home_team, away_team;int home_score, away_score;int i, j;for(i = 0; i TEAMCOUNT/2; i+)coutInput the name of home/away team and the score of home/away team:home_teamaway_teamhome_scoreaway_score;for(j = 0; j TEAMCOUNT; j+)if(tmj.team_name = home_team)tmj.
8、change(home_score, away_score);if(tmj.team_name = away_team)tmj.change(away_score, home_score);void sort()int i, j, k;team temp(1);for(i = 0; i TEAMCOUNT; i+) k = i;for(j = i+1; j TEAMCOUNT; j+)if(tmk.accumulate_points tmj.accumulate_points)k = j;if(k!=i)temp = tmi;tmi = tmk;tmk = temp;void display(
9、)int i;for(i = 0; i TEAMCOUNT; i+)cout 第i+1名: tmi.team_name 积分: tmi.accumulate_pointsendl;void main()int i;for(i = 0; i TEAMCOUNT-1; i+)input_score();sort();display();(课本413面第4题)编写一个程序,声明抽象定义对象时给定。#include using namespace std;/定义抽象基类Shapeclass Shapepublic: virtual double area() const =0; /纯虚函数;/定义Ci
10、rcle类class Circle:public Shapepublic:Circle(double r):radius(r) /结构函数 virtual double area() const return 3.14159*radius*radius; /定义虚函 数 protected: double radius; /半径;/定义Rectangle类class Rectangle:public Shapepublic: Rectangle(double w,double h):width(w),height(h) /结构函数 virtual double area() const ret
11、urn width*height; /定义虚函 数 protected: double width,height; /宽与高;class Triangle:public Shapepublic: Triangle(double w,double h):width(w),height(h) /结构函 数 virtual double area() const return 0.5*width*height; /定义虚 函数 protected: double width,height; /宽与高;/输出面积的函数void printArea(const Shape &s)couts.area()
12、endl; /输出s 的面积int main() Circle circle(12.6); /建立 Circle类对象circle coutarea of circle =; printArea(circle); /输出 circle的面积 Rectangle rectangle(4.5,8.4); /建立 Rectangle类对象rectangle coutarea of rectangle =; printArea(rectangle); /输出 rectangle的面积 Triangle triangle(4.5,8.4); /建立 Triangle类对象 coutarea of tri
13、angle =; printArea(triangle); /输出triangle的面积 return 0;(课本414面第5题)编写一个程序一个派生类对象。#include using namespace std;/定义抽象基类Shapeclass Shapepublic: virtual double area() const =0; /纯虚函数;/定义Circle(圆形)类class Circle:public Shapepublic:Circle(double r):radius(r) /结构函数 virtual double area() const return 3.14159*r
14、adius*radius; /定义虚函 数 protected: double radius; /半径;/定义Square(正方形)类class Square:public Shapepublic: Square(double s):side(s) /结构函数 virtual double area() const return side*side; /定义虚函 数 protected: double side;/定义Rectangle(矩形)类class Rectangle:public Shapepublic: Rectangle(double w,double h):width(w),h
15、eight(h) /结构函数 virtual double area() const return width*height; /定义虚函 数 protected: double width,height; /宽与高;/定义Trapezoid(梯形)类class Trapezoid:public Shapepublic: Trapezoid(double t,double b,double h):top(t),bottom(t),height(h) /结构 函数 virtual double area() const return 0.5*(top+bottom)*height; /定义 虚函
16、数 protected: double top,bottom,height; /上底 、下底与高;/定义Triangle(三角形)类class Triangle:public Shapepublic: Triangle(double w,double h):width(w),height(h) /结构函 数 virtual double area() const return 0.5*width*height; /定义虚 函数 protected: double width,height; /宽与高;int main() Circle circle(12.6); /建立 Circle类对象ci
17、rcle Square square(3.5); /建立 Square类对象square Rectangle rectangle(4.5,8.4); /建立 Rectangle类对象rectangle Trapezoid trapezoid(2.0,4.5,3.2); /建立 Trapezoid类对象trapezoid Triangle triangle(4.5,8.4); /建立 Triangle类对象 Shape *pt5=&circle,&square,&rectangle,&trapezoid,▵ /定义基类指针数组pt,使它每 一个元素指向一个派生类对象 doubl
18、e areas=0.0; /areas为 总面积 for(int i=0;iarea(); couttotol of all areas=areasendl; /输出总面积 return 0;实验十五 动物怎么叫?好玩中理解抽象类 【题目】下面给出了基类Animal和main()函数。(任务2.1)根据main()函数给出的注释提示,设计出相关的各个类。(任务2.2)显然,Animal设计为抽象类更合适,Animal不需要能够实例化,是专门作基类使用的。改造程序,使Animal设计为抽象类,这时main()函数中p =newAnimal();将出错,将此行删除。(任务2.3)每一个Animal
19、的派生类都有一个“名字”数据成员,改造上面的程序,将这一数据成员作为抽象类Animal数据成员被各派生类使用。cpp 1. class Animal 2. 3. public: 4. virtual void cry()cout不知哪种动物,让我如何学叫?cry(); /输出: 不知哪种动物,让我如何学叫?(问题出自此处) 11. Mouse m(Jerry); p=&m; p-cry(); /输出: 我叫Jerry,是一只老鼠,我的叫声是:吱吱吱! 12. Cat c(Tom); p=&c; p-cry(); /输出: 我叫Tom,是一只猫,我的叫声是:喵喵喵! 13. Dog d(Droo
20、py); p=&d; p-cry(); /输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪! 14. Giraffe g(Gill); p=&g; p-cry(); /输出: 我叫Gill,是长颈鹿,脖子太长,发不出声音来! 15. system(pause); 16. return 0; 17. 参考解答(任务2.1)根据main()函数给出的注释提示,设计出相关的各个类。cpp 1. #include iostream 2. #include 3. using namespacestd; 4. class Animal 5. 6. public: 7. virtual void cr
21、y() cout不知哪种动物,让我如何学叫?endl; 8. ; 9. 10. class Mouse : publicAnimal 11. 12. private: 13. string name; 14. public: 15. Mouse(string nam):name(nam) 16. virtual void cry() cout我叫name,是一只老鼠,我的叫声是:吱吱吱!endl; 17. ; 18. 19. class Cat : publicAnimal 20. 21. private: 22. string name; 23. public: 24. Cat(string
22、 nam):name(nam) 25. virtual void cry() cout我叫name,是一只猫,我的叫声是:喵喵喵!endl; 26. ; 27. 28. class Dog : publicAnimal 29. 30. private: 31. string name; 32. public: 33. Dog(string nam):name(nam) 34. virtual void cry() cout我叫name,是一条狗,我的叫声是:汪汪汪!endl; 35. ; 36. 37. class Giraffe : publicAnimal 38. 39. private:
23、 40. string name; 41. public: 42. Giraffe(string nam):name(nam) 43. virtual void cry() cout我叫name,是长颈鹿,我的脖子太长,发不出声音来!cry(); /输出: 不知哪种动物,让我如何学叫?(问题出自此处) 50. Mouse m(Jerry);p=&m; p-cry(); /输出: 我叫Jerry,是一只老鼠,我的叫声是:吱吱吱! 51. Cat c(Tom); p=&c; p-cry(); /输出: 我叫Tom,是一只猫,我的叫声是:喵喵喵! 52. Dog d(Droopy); p=&d; p
24、-cry(); /输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪! 53. Giraffe g(Gill); p=&g; p-cry(); /输出: 我叫Gill,是长颈鹿,脖子太长,发不出声音来! 54. system(pause); 55. return 0; 56. (任务2.2)显然,Animal设计为抽象类更合适,Animal不需要能够实例化,是专门作基类使用的。改造程序,使Animal设计为抽象类,这时main()函数中p =newAnimal();将出错,将此行删除。cpp 1. #include iostream 2. #include 3. using namespa
25、cestd; 4. class Animal 5. 6. public: 7. virtual void cry() = 0; /纯虚函数,使Animal成为了抽象类 8. ; 9. 10. class Mouse : public Animal 11. 12. private: 13. string name; 14. public: 15. Mouse(string nam):name(nam) 16. virtual void cry() cout我叫name,是一只老鼠,我的叫声是:吱吱吱!endl; 17. ; 18. 19. class Cat : public Animal 20. 21. private: 22. string name; 23. public: 24. Cat(string nam):name(nam) 25. virtual void cry() cout我叫name,是一只猫,我的叫声是:喵喵喵!endl; 26. ; 27. 28. class Dog : public Animal 29. 30. private: 31. string name; 32. public: 33. Dog(string nam):name(nam) 34. virtual void cry() cout我叫name
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2