C++考试

发布时间:2015-02-10 17:39:23

0—1背包问题

#include

#include

#include

int min(int w,int c)

{int temp;

if (w

else

temp=c;

return temp;

}

int max(int w,int c)

{

int temp;

if (w>c) temp=w;

else

temp=c;

return temp;

}

void knapsack(int v[],int w[],int c,int n,int**m) //求最优值

{

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

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

m[n][j]=0;

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

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

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

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

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

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

for(int jj=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(int l=2;l<=n;l++)

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

{

cout<

}

cout<

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

}

int traceback(int **m,int w[],int c,int n,int x[]) //回代,求最优解

{

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

for(int i=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(int y=1;y<=n;y++)

{

cout<

}

return x[n];

}

void main()

{

int n,c;

int **m;

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

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

cin>>n>>c;

int *v=new int[n+1];

cout<<"Pls input the property (v[i]):"<

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

cin>>v[i];

int *w=new int[n+1];

cout<<"Pls input the weight (w[i]):"<

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

cin>>w[j];

int *x=new int[n+1];

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

for(int p=0;p

{

m[p]=new int[c+1];

}

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

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

}

循环赛制

#include
#include
#define TEAMCOUNT 4
using 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)
{
cout<<"Input the name of the "<<++team_count<< " team:"<
cin>>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 accumulate_points; //总积分
static int team_count; //球队数
};
int team::team_count = 0;
team tm[TEAMCOUNT]; //球队数组
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++;
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++)
{
cout<<"Input the name of home/away team and the score of home/away team:"<
cin>>home_team>>away_team>>home_score>>away_score;
for(j = 0; j < TEAMCOUNT; 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);
}
}
}

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(tm[k].accumulate_points < tm[j].accumulate_points)
k = j;
if(k!=i)
{
temp = tm[i];
tm[i] = tm[k];
tm[k] = temp;
}
}
}

void display()
{
int i;
for(i = 0; i < TEAMCOUNT; i++)
cout<<" "<: "<积分: "<
}

void main()
{
int i;
for(i = 0; i < TEAMCOUNT-1; i++)
{
input_score();
sort();
display();
}
}
(课本413面第4题)编写一个程序,声明抽象······定义对象时给定。

#include

using namespace std;

//定义抽象基类Shape

class Shape

{public:

virtual double area() const =0; //纯虚函数

};

//定义Circle

class Circle:public Shape

{public:

Circle(double r):radius(r){} //结构函数

virtual double area() const {return 3.14159*radius*radius;}; //定义虚函

protected:

double radius; //半径

};

//定义Rectangle

class Rectangle:public Shape

{public:

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

virtual double area() const {return width*height;} //定义虚函

protected:

double width,height; //宽与高

};

class Triangle:public Shape

{public:

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)

{cout<输出s

的面积

int main()

{

Circle circle(12.6); //建立

Circle类对象circle

cout<<"area of circle =";

printArea(circle); //输出

circle的面积

Rectangle rectangle(4.5,8.4); //建立

Rectangle类对象rectangle

cout<<"area of rectangle =";

printArea(rectangle); //输出

rectangle的面积

Triangle triangle(4.5,8.4); //建立

Triangle类对象

cout<<"area of triangle =";

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

return 0;

}

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

#include

using namespace std;

//定义抽象基类Shape

class Shape

{public:

virtual double area() const =0; //纯虚函数

};

//定义Circle(圆形)

class Circle:public Shape

{public:

Circle(double r):radius(r){} //结构函数

virtual double area() const {return 3.14159*radius*radius;}; //定义虚函

protected:

double radius; //半径

};

//定义Square(正方形)

class Square:public Shape

{public:

Square(double s):side(s){} //结构函数

virtual double area() const {return side*side;} //定义虚函

protected:

double side;

};

//定义Rectangle(矩形)

class Rectangle:public Shape

{public:

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

virtual double area() const {return width*height;} //定义虚函

protected:

double width,height; //宽与高

};

//定义Trapezoid(梯形)

class Trapezoid:public Shape

{public:

Trapezoid(double t,double b,double h):top(t),bottom(t),height(h){} //结构

函数

virtual double area() const {return 0.5*(top+bottom)*height;} //定义

虚函数

protected:

double top,bottom,height; //上底

、下底与高

};

//定义Triangle(三角形)

class Triangle:public Shape

{public:

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类对象circle

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 *pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle};

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

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

double areas=0.0; //areas

总面积

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

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

cout<<"totol of all areas="<输出总面积

return 0;

}实验十五 动物怎么叫?——好玩中理解抽象类

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

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

(任务2.2)显然,Animal设计为抽象类更合适,Animal不需要能够实例化,是专门作基类使用的。改造程序,使Animal设计为抽象类,这时main()函数中p =newAnimal();将出错,将此行删除。

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

[cpp]

1. class Animal

2. {

3. public:

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

5. };

6.

7. int main( )

8. {

9. Animal *p;

10. p = new Animal(); p->cry(); //输出: 不知哪种动物,让我如何学叫?(问题出自此处)

11. Mouse m("Jerry"); p=&m; p->cry(); //输出: 我叫Jerry,是一只老鼠,我的叫声是:吱吱吱!

12. Cat c("Tom"); p=&c; p->cry(); //输出: 我叫Tom,是一只猫,我的叫声是:喵喵喵!

13. Dog d("Droopy"); 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 cry() {cout<<"不知哪种动物,让我如何学叫?"<

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<<"我叫"<",是一只老鼠,我的叫声是:吱吱吱!"<

17. };

18.

19. class Cat : publicAnimal

20. {

21. private:

22. string name;

23. public:

24. Cat(string nam):name(nam){}

25. virtual void cry() {cout<<"我叫"<",是一只猫,我的叫声是:喵喵喵!"<

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<<"我叫"<",是一条狗,我的叫声是:汪汪汪!"<

35. };

36.

37. class Giraffe : publicAnimal

38. {

39. private:

40. string name;

41. public:

42. Giraffe(string nam):name(nam){}

43. virtual void cry() {cout<<"我叫"<",是长颈鹿,我的脖子太长,发不出声音来!"<

44. };

45.

46. int main( )

47. {

48. Animal *p;

49. p = newAnimal(); p->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->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 namespacestd;

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<<"我叫"<",是一只老鼠,我的叫声是:吱吱吱!"<

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<<"我叫"<",是一只猫,我的叫声是:喵喵喵!"<

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<<"我叫"<",是一条狗,我的叫声是:汪汪汪!"<

35. };

36.

37. class Giraffe : public Animal

38. {

39. private:

40. string name;

41. public:

42. Giraffe(string nam):name(nam){}

43. virtual void cry() {cout<<"我叫"<",是长颈鹿,我的脖子太长,发不出声音来!"<

44. };

45.

46. int main( )

47. {

48. Animal *p;

49. //p = newAnimal(); p->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->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪!

53. Giraffe g("Gill"); p=&g; p->cry(); //输出: 我叫Gill,是长颈鹿,脖子太长,发不出声音来!

54. system("pause");

55. return 0;

56. }

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

[cpp]

1. #include "iostream"

2. #include

3. using namespacestd;

4.

5. class Animal

6. {

7. protected:

8. string name; //类族中的数据成员放在这儿

9. public:

10. Animal(string nam):name(nam){} //基类的构造函数

11. virtual void cry()=0;

12. };

13.

14. class Mouse : publicAnimal

15. {

16. public:

17. Mouse(string nam):Animal(nam){} //派生类的构造函数要这样写

18. virtual void cry() {cout<<"我叫"<",是一只老鼠,我的叫声是:吱吱吱!"<

19. };

20.

21. class Cat : publicAnimal

22. {

23. public:

24. Cat(string nam):Animal(nam){}

25. virtual void cry() {cout<<"我叫"<",是一只猫,我的叫声是:喵喵喵!"<

26. };

27.

28. class Dog : publicAnimal

29. {

30. public:

31. Dog(string nam):Animal(nam){}

32. virtual void cry() {cout<<"我叫"<",是一条狗,我的叫声是:汪汪汪!"<

33. };

34.

35. class Giraffe : publicAnimal

36. {

37. public:

38. Giraffe(string nam):Animal(nam){}

39. virtual void cry() {cout<<"我叫"<",是长颈鹿,我的脖子太长,发不出声音来!"<

40. };

41.

42. int main( )

43. {

44. Animal *p;

45. //p = newAnimal(); p->cry(); //抽象类不能实例化对象了!!!

46. Mouse m("Jerry");p=&m; p->cry(); //输出: 我叫Jerry,是一只老鼠,我的叫声是:吱吱吱!

47. Cat c("Tom"); p=&c; p->cry(); //输出: 我叫Tom,是一只猫,我的叫声是:喵喵喵!

48. Dog d("Droopy"); p=&d; p->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪!

49. Giraffe g("Gill"); p=&g; p->cry(); //输出: 我叫Gill,是长颈鹿,我的脖子太长,发不出声音来!

50. system("pause");

51. return 0;

52. }

实验十六 构造数组

【题目】建立专门的数组类处理有关数组的操作
在下面代码的基础上,完成支持数组操作的类的设计,增强C++内置数组类型功能。

1. class My

2. {

3. private:

4. int *arr; //用于存放动态分配的数组内存首地址

5. int size; //数组大小

6. public:

7. My(int sz=50);

8. My(int a[],int sz); //由一个内置类型的数组初始化

9. My(const My &A); //复制构造函数

10. ~My(void); //析构函数,注意释放空间

11. My&operator =(const My &A); //重载“=”使得数组对象可以整体赋值

12. int& operator[](int i); //重载[],使得对象也可以如C++普通数组一样,用a[i]形式取出值【选做】

13. bool operator == (My& A); //重载==,使得对象能整体判断两个数组是否相等(size相等且对应元素相等)

14. My operator + (My& A); //重载+,使两个对象可以整体相加(前提大小相等)【选做】

15. friend ostream& operator << (ostream& out,My& A); //重载<<,输出数组

16. int GetSize(void)const; //取数组大小;

17. void Resize(int sz); //修改数组的大小,如果sz大于数组的原大小,增加的元素初始为;sz小于数组的原大小,舍弃后面的元素【选做】

18. };

19.

20. int main()

21. { int a[10]={1,2,3,4,5,6,7,8,9,10};

22. int b[10]={4,5,6,7,8,9,10,11,12,13};

23. My arr1(a,10);

24. My arr2(b,10);

25. My arr3(10);

26. cout<

27. arr3 = arr1 +arr2;

28. cout<

29. arr3.Resize(20);

30. cout<

31. arr3.Resize(5);

32. cout<

33. system("pause");

34. return 0;

35. }

【参考解答】

1. #include

2. using namespace std;

3. class My

4. {

5. private:

6. int *arr; //用于存放动态分配的数组内存首地址

7. int size; //数组大小

8. public:

9. My(int sz=50);

10. My(int a[],int sz); //由一个内置类型的数组初始化

11. My(const My &A); //复制构造函数

12. ~My(void); //析构函数,注意释放空间

13. My&operator =(const My &A); //重载“=”使得数组对象可以整体赋值

14. int& operator[](int i); //重载[],使得对象也可以如C++普通数组一样,用a[i]形式取出值【选做】

15. bool operator == (My& A); //重载==,使得对象能整体判断两个数组是否相等(size相等且对应元素相等)

16. My operator + (My& A); //重载+,使两个对象可以整体相加(前提大小相等)【选做】

17. friend ostream& operator << (ostream& out,My& A); //重载<<,输出数组

18. int GetSize(void)const; //取数组大小;

19. void Resize(int sz); //修改数组的大小,如果sz大于数组的原大小,增加的元素初始为;如果sz大于数组的原大小,舍弃后面的元素【选做】

20. };

21.

22. //以下为类成员函数的定义

23.

24. //构造函数

25. My::My(int sz)

26. {

27. if(sz<=0)

28. {

29. cout<<"invalid Size!";

30. exit(1);

31. }

32. size=sz; //将元素个数赋值给变量size

33. arr=new int[size];//动态分配内存,将sizeint类型的元素空间分配出来

34. for(int i=0; i

35. *(arr+i)=0;

36. }

37.

38. My::My(int a[],int sz)

39. {

40. if(sz<=0)

41. {

42. cout<<"invalidSize";

43. exit(1);

44. }

45. size=sz;//将元素个数赋值给变量size

46. arr=new int[size];//动态分配内存,将sizeint类型的元素空间分配出来

47. for(int i=0; i

48. *(arr+i)=*(a+i);

49. }

50.

51. //析构函数

52. My::~My(void)

53. {

54. delete []arr;

55. }

56.

57. //复制构造函数

58. My::My(const My& A)

59. {

60. //从对象A取得数组大小,并赋给当前对象成员

61. int n=A.size;

62. size=n;

63. //为对象申请内存并进行出错检测

64. arr=new int[n];

65. //从对象A复制数组元素到本对象

66. int *srcptr=A.arr;//X.arr是对象A的数组首地址

67. int *destptr=arr;//arr是本对象中的数组首地址

68. while(n--)

69. {

70. *destptr=*srcptr;

71. destptr++;

72. srcptr++;

73. }

74. }

75.

76. //重载'='

77. My& My::operator =(const My &A)

78. {

79. int n=A.size;//A数组的大小

80. //如果本对象中的数组大小和A不同,则删除数组原有的内存,然后重新分配

81. if (size!=n)

82. {

83. delete []arr;

84. arr=new int[n];

85. size=n;

86. }

87.

88. //rhs向本对象复制元素

89. int* destptr=arr;

90. int* srcptr=A.arr;

91. while(n--)

92. {

93. *destptr=*srcptr;

94. destptr++;

95. srcptr++;

96. }

97. return *this;//返回当前对象的引用

98. }

99.

100. //重载[]

101. int &My::operator[](int n)

102. {

103. if(n<0||n>size-1)

104. {

105. cout<<"indexOutOfRange"<

106. exit(1);

107. }

108. return arr[n];

109. }

110.

111. bool My::operator == (My& A)

112. {

113. bool eq=true;

114. int n=A.size; //A数组的大小

115. if (size!=n) //大小是否一致

116. {

117. eq=false;

118. }

119. else

120. {

121. int* destptr=arr;

122. int* srcptr=A.arr;

123. while(n--)

124. {

125. if(*destptr!=*srcptr)

126. {

127. eq=false;

128. break;

129. }

130. destptr++;

131. srcptr++;

132. }

133. }

134. return eq;//返回当前对象的引用

135. }

136.

137. My My::operator + (My& A)

138. {

139. int n=A.size; //A数组的大小

140. if (size!=n) //大小不一致不能相加

141. {

142. cout<<"not same size for add!"<

143. exit(1);

144. }

145. My a(n); //指定size的数组

146.

147. for (int i = 0; i < size; i++)

148. {

149. a[i]=arr[i]+A[i];

150. }

151. return a;//返回当前对象的引用

152. }

153.

154. ostream& operator << (ostream& out, My& A)

155. {

156. for (int i = 0; i < A.size; i++)

157. {

158. out<'\t';

159. }

160. out<

161. return out;

162. }

163.

164. //取当前数组大小

165. int My::GetSize(void)const

166. {

167. return size;

168. }

169.

170. //将数组大小修改为sz

171. void My::Resize(int sz)

172. {

173. if(sz<=0)

174. {

175. cout<<"invalidSize"<

176. exit(1);

177. }

178. if(sz==size)

179. return;

180. int *newlist=new int[sz];

181. for (int i = 0; i < sz; i++)

182. {

183. *(newlist+i)=0;

184. }

185. int n=(sz<=size)?sz:size;

186. int *srcptr=arr;

187. int *destptr=newlist;

188. while(n--)

189. {

190. *destptr=*srcptr;

191. destptr++;

192. srcptr++;

193. }

194. delete []arr;

195. arr=newlist;

196. size=sz;

197. }

198.

199. int main()

200. {

201. int a[10]={1,2,3,4,5,6,7,8,9,10};

202. int b[10]={4,5,6,7,8,9,10,11,12,13};

203. My arr1(a,10);

204. My arr2(b,10);

205. My arr3(10);

206. cout<

207. arr3 = arr1 +arr2;

208. cout<

209. arr3.Resize(20);

210. cout<

211. arr3.Resize(5);

212. cout<

213. system("pause");

214. return 0;

215. }

C++考试

相关推荐