C ++考试.docx

上传人:b****1 文档编号:14052649 上传时间:2023-06-20 格式:DOCX 页数:31 大小:31.39KB
下载 相关 举报
C ++考试.docx_第1页
第1页 / 共31页
C ++考试.docx_第2页
第2页 / 共31页
C ++考试.docx_第3页
第3页 / 共31页
C ++考试.docx_第4页
第4页 / 共31页
C ++考试.docx_第5页
第5页 / 共31页
C ++考试.docx_第6页
第6页 / 共31页
C ++考试.docx_第7页
第7页 / 共31页
C ++考试.docx_第8页
第8页 / 共31页
C ++考试.docx_第9页
第9页 / 共31页
C ++考试.docx_第10页
第10页 / 共31页
C ++考试.docx_第11页
第11页 / 共31页
C ++考试.docx_第12页
第12页 / 共31页
C ++考试.docx_第13页
第13页 / 共31页
C ++考试.docx_第14页
第14页 / 共31页
C ++考试.docx_第15页
第15页 / 共31页
C ++考试.docx_第16页
第16页 / 共31页
C ++考试.docx_第17页
第17页 / 共31页
C ++考试.docx_第18页
第18页 / 共31页
C ++考试.docx_第19页
第19页 / 共31页
C ++考试.docx_第20页
第20页 / 共31页
亲,该文档总共31页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C ++考试.docx

《C ++考试.docx》由会员分享,可在线阅读,更多相关《C ++考试.docx(31页珍藏版)》请在冰点文库上搜索。

C ++考试.docx

C++考试

0—1背包问题

#include

#include

#include

intmin(intw,intc)

{inttemp;

if(w

else

temp=c;

returntemp;

}

intmax(intw,intc)

{

inttemp;

if(w>c)temp=w;

else

temp=c;

returntemp;

}

voidknapsack(intv[],intw[],intc,intn,int**m)//求最优值

{

intjmax=min(w[n]-1,c);

for(intj=0;j<=jmax;j++)

m[n][j]=0;

for(intjj=w[n];jj<=c;jj++)

m[n][jj]=v[n];

for(inti=n-1;i>1;i--){//递归部分

jmax=min(w[i]-1,c);

for(intj=0;j<=jmax;j++)

m[i][j]=m[i+1][j];

for(intjj=w[i];jj<=c;jj++)

m[i][jj]=max(m[i+1][jj],m[i+1][jj-w[i]]+v[i]);

}

m[1][c]=m[2][c];

if(c>=w[1])

m[1][c]=max(m[1][c],m[2][c-w[1]]+v[1]);

cout<<"最优值:

"<

for(intl=2;l<=n;l++)

for(intj=0;j<=c;j++)

{

cout<

}

cout<

cout<<"*******************************************"<

}

inttraceback(int**m,intw[],intc,intn,intx[])//回代,求最优解

{

cout<<"得到的一组最优解如下:

"<

for(inti=1;i

if(m[i][c]==m[i+1][c])x[i]=0;

else{x[i]=1;

c-=w[i];}

x[n]=(m[n][c])?

1:

0;

for(inty=1;y<=n;y++)

{

cout<

}

returnx[n];

}

voidmain()

{

intn,c;

int**m;

cout<<"&&&&&&&&&&&&&&&&&&&&&欢迎使用0-1背包问题程序&&&&&&&&&&&&&&&&&&&"<

cout<<"请输入物品个数和重量上限:

";

cin>>n>>c;

int*v=newint[n+1];

cout<<"Plsinputtheproperty(v[i]):

"<

for(inti=1;i<=n;i++)

cin>>v[i];

int*w=newint[n+1];

cout<<"Plsinputtheweight(w[i]):

"<

for(intj=1;j<=n;j++)

cin>>w[j];

int*x=newint[n+1];

m=newint*[n+1];//动态的分配二维数组

for(intp=0;p

{

m[p]=newint[c+1];

}

knapsack(v,w,c,n,m);

traceback(m,w,c,n,x);

}

循环赛制

#include

#include

#defineTEAMCOUNT4

usingnamespacestd;

classteam//定义一个球队类

{

public:

team():

paly_amount(0),win_amount(0),tie_amount(0),lose_amount(0),in_amount(0),out_amount(0),accumulate_points(0)

{

cout<<"Inputthenameofthe"<<++team_count<<"team:

"<

cin>>team_name;

}

team(int){}

voidchange(intmine,intopponent);

friendvoidinput_score();

friendvoidsort();

friendvoiddisplay();

private:

stringteam_name;//队名

intpaly_amount;//比赛场数

intwin_amount;//赢的场数

inttie_amount;//平的场数

intlose_amount;//输的场数

intin_amount;//进球数

intout_amount;//失球数

intaccumulate_points;//总积分

staticintteam_count;//球队数

};

intteam:

:

team_count=0;

teamtm[TEAMCOUNT];//球队数组

voidteam:

:

change(intmine,intopponent)

{

paly_amount++;

in_amount+=mine;

lose_amount+=opponent;

if(mine>opponent)

{

win_amount++;

accumulate_points+=3;

}

elseif(mine

{

lose_amount++;

}

else

{

tie_amount++;

accumulate_points+=1;

}

}

voidinput_score()

{

stringhome_team,away_team;

inthome_score,away_score;

inti,j;

for(i=0;i

{

cout<<"Inputthenameofhome/awayteamandthescoreofhome/awayteam:

"<

cin>>home_team>>away_team>>home_score>>away_score;

for(j=0;j

{

if(tm[j].team_name==home_team)

tm[j].change(home_score,away_score);

if(tm[j].team_name==away_team)

tm[j].change(away_score,home_score);

}

}

}

voidsort()

{

inti,j,k;

teamtemp

(1);

for(i=0;i

{

k=i;

for(j=i+1;j

if(tm[k].accumulate_points

k=j;

if(k!

=i)

{

temp=tm[i];

tm[i]=tm[k];

tm[k]=temp;

}

}

}

voiddisplay()

{

inti;

for(i=0;i

cout<<"第"<

"<

"<

}

voidmain()

{

inti;

for(i=0;i

{

input_score();

sort();

display();

}

}

(课本413面第4题)编写一个程序,声明抽象······定义对象时给定。

#include

usingnamespacestd;

//定义抽象基类Shape

classShape

{public:

virtualdoublearea()const=0;//纯虚函数

};

//定义Circle类

classCircle:

publicShape

{public:

Circle(doubler):

radius(r){}//结构函数

virtualdoublearea()const{return3.14159*radius*radius;};//定义虚函

protected:

doubleradius;//半径

};

//定义Rectangle类

classRectangle:

publicShape

{public:

Rectangle(doublew,doubleh):

width(w),height(h){}//结构函数

virtualdoublearea()const{returnwidth*height;}//定义虚函

protected:

doublewidth,height;//宽与高

};

classTriangle:

publicShape

{public:

Triangle(doublew,doubleh):

width(w),height(h){}//结构函

virtualdoublearea()const{return0.5*width*height;}//定义虚

函数

protected:

doublewidth,height;//宽与高

};

//输出面积的函数

voidprintArea(constShape&s)

{cout<

的面积

intmain()

{

Circlecircle(12.6);//建立

Circle类对象circle

cout<<"areaofcircle=";

printArea(circle);//输出

circle的面积

Rectanglerectangle(4.5,8.4);//建立

Rectangle类对象rectangle

cout<<"areaofrectangle=";

printArea(rectangle);//输出

rectangle的面积

Triangletriangle(4.5,8.4);//建立

Triangle类对象

cout<<"areaoftriangle=";

printArea(triangle);//输出triangle的面积

return0;

}

(课本414面第5题)编写一个程序······一个派生类对象。

#include

usingnamespacestd;

//定义抽象基类Shape

classShape

{public:

virtualdoublearea()const=0;//纯虚函数

};

//定义Circle(圆形)类

classCircle:

publicShape

{public:

Circle(doubler):

radius(r){}//结构函数

virtualdoublearea()const{return3.14159*radius*radius;};//定义虚函

protected:

doubleradius;//半径

};

//定义Square(正方形)类

classSquare:

publicShape

{public:

Square(doubles):

side(s){}//结构函数

virtualdoublearea()const{returnside*side;}//定义虚函

protected:

doubleside;

};

//定义Rectangle(矩形)类

classRectangle:

publicShape

{public:

Rectangle(doublew,doubleh):

width(w),height(h){}//结构函数

virtualdoublearea()const{returnwidth*height;}//定义虚函

protected:

doublewidth,height;//宽与高

};

//定义Trapezoid(梯形)类

classTrapezoid:

publicShape

{public:

Trapezoid(doublet,doubleb,doubleh):

top(t),bottom(t),height(h){}//结构

函数

virtualdoublearea()const{return0.5*(top+bottom)*height;}//定义

虚函数

protected:

doubletop,bottom,height;//上底

、下底与高

};

//定义Triangle(三角形)类

classTriangle:

publicShape

{public:

Triangle(doublew,doubleh):

width(w),height(h){}//结构函

virtualdoublearea()const{return0.5*width*height;}//定义虚

函数

protected:

doublewidth,height;//宽与高

};

intmain()

{

Circlecircle(12.6);//建立

Circle类对象circle

Squaresquare(3.5);//建立

Square类对象square

Rectanglerectangle(4.5,8.4);//建立

Rectangle类对象rectangle

Trapezoidtrapezoid(2.0,4.5,3.2);//建立

Trapezoid类对象trapezoid

Triangletriangle(4.5,8.4);//建立

Triangle类对象

Shape*pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle};

//定义基类指针数组pt,使它每

一个元素指向一个派生类对象

doubleareas=0.0;//areas为

总面积

for(inti=0;i<5;i++)

{areas=areas+pt[i]->area();}

cout<<"totolofallareas="<

return0;

}实验十五动物怎么叫?

——好玩中理解抽象类

【题目】下面给出了基类Animal和main()函数。

(任务2.1)根据main()函数给出的注释提示,设计出相关的各个类。

(任务2.2)显然,Animal设计为抽象类更合适,Animal不需要能够实例化,是专门作基类使用的。

改造程序,使Animal设计为抽象类,这时main()函数中p=newAnimal();将出错,将此行删除。

(任务2.3)每一个Animal的派生类都有一个“名字”数据成员,改造上面的程序,将这一数据成员作为抽象类Animal数据成员被各派生类使用。

[cpp]

1.classAnimal

2.{

3.public:

4.virtualvoidcry(){cout<<"不知哪种动物,让我如何学叫?

"<

5.};

6.

7.intmain()

8.{

9.Animal*p;

10.p=newAnimal();p->cry();//输出:

不知哪种动物,让我如何学叫?

(问题出自此处)

11.Mousem("Jerry");p=&m;p->cry();//输出:

我叫Jerry,是一只老鼠,我的叫声是:

吱吱吱!

12.Catc("Tom");p=&c;p->cry();//输出:

我叫Tom,是一只猫,我的叫声是:

喵喵喵!

13.Dogd("Droopy");p=&d;p->cry();//输出:

我叫Droopy,是一条狗,我的叫声是:

汪汪汪!

14.Giraffeg("Gill");p=&g;p->cry();//输出:

我叫Gill,是长颈鹿,脖子太长,发不出声音来!

15.system("pause");

16.return0;

17.}

参考解答

(任务2.1)根据main()函数给出的注释提示,设计出相关的各个类。

[cpp]

1.#include"iostream"

2.#include

3.usingnamespacestd;

4.classAnimal

5.{

6.public:

7.virtualvoidcry(){cout<<"不知哪种动物,让我如何学叫?

"<

8.};

9.

10.classMouse:

publicAnimal

11.{

12.private:

13.stringname;

14.public:

15.Mouse(stringnam):

name(nam){}

16.virtualvoidcry(){cout<<"我叫"<

吱吱吱!

"<

17.};

18.

19.classCat:

publicAnimal

20.{

21.private:

22.stringname;

23.public:

24.Cat(stringnam):

name(nam){}

25.virtualvoidcry(){cout<<"我叫"<

喵喵喵!

"<

26.};

27.

28.classDog:

publicAnimal

29.{

30.private:

31.stringname;

32.public:

33.Dog(stringnam):

name(nam){}

34.virtualvoidcry(){cout<<"我叫"<

汪汪汪!

"<

35.};

36.

37.classGiraffe:

publicAnimal

38.{

39.private:

40.stringname;

41.public:

42.Giraffe(stringnam):

name(nam){}

43.virtualvoidcry(){cout<<"我叫"<

"<

44.};

45.

46.intmain()

47.{

48.Animal*p;

49.p=newAnimal();p->cry();//输出:

不知哪种动物,让我如何学叫?

(问题出自此处)

50.Mousem("Jerry");p=&m;p->cry();//输出:

我叫Jerry,是一只老鼠,我的叫声是:

吱吱吱!

51.Catc("Tom");p=&c;p->cry();//输出:

我叫Tom,是一只猫,我的叫声是:

喵喵喵!

52.Dogd("Droopy");p=&d;p->cry();//输出:

我叫Droopy,是一条狗,我的叫声是:

汪汪汪!

53.Giraffeg("Gill");p=&g;p->cry();//输出:

我叫Gill,是长颈鹿,脖子太长,发不出声音来!

54.system("pause");

55.return0;

56.}

(任务2.2)显然,Animal设计为抽象类更合适,Animal不需要能够实例化,是专门作基类使用的。

改造程序,使Animal设计为抽象类,这时main()函数中p=newAnimal();将出错,将此行删除。

[cpp]

1.#include"iostream"

2.#include

3.usingnamespacestd;

4.classAnimal

5.{

6.public:

7.virtualvoidcry()=0;//纯虚函数,使Animal成为了抽象类

8.};

9.

10.classMouse:

publicAnimal

11.{

12.private:

13.stringname;

14.public:

15.Mouse(stringnam):

name(nam){}

16.virtualvoidcry(){cout<<"我叫"<

吱吱吱!

"<

17.};

18.

19.classCat:

publicAnimal

20.{

21.private:

22.stringname;

23.public:

24.Cat(stringnam):

name(nam){}

25.virtualvoidcry(){cout<<"我叫"<

喵喵喵!

"<

26.};

27.

28.classDog:

publicAnimal

29.{

30.private:

31.stringname;

32.public:

33.Dog(stringnam):

name(nam){}

34.virtualvoidcry(){cout<<"我叫"<

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 文学研究

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2