A.单选题
2-1下列程序代码,正确的输出结果是( )
int a=0xfe; char b=062; cout<
A.
0xfe,062
B.
fe,62
C.
254,2
D.
254,62
2-2下列代码能正确执行的是( )
A.
char a='C'; const char& p=a;
B.
char a='C'; char* const p; p=&a;
C.
char a='C'; const char& p; p=a;
D.
char a='C'; char& p; p=a;
2-3
new和delete运算符正确的使用是( )
A.
int* p=new int[10]; delete p;
B.
int* p=new int(10); delete []p;
C.
vector
* v=new vector [10]; delete []v; D.
int* a=new int[]{1,2,3,4}; delete []a;
2-4
下列哪个代码能够实现x和y值的交换( )
A.
void fun(int a,int b) {int x = a;a = b;b = x; } int main() {int x = 1, y = 2;fun(&x, &y);cout << x << ","<
B.
void fun(int* a,int* b) {int x = *a;*a = *b;*b = x; } int main() {int x = 1, y = 2;fun(&x,&y);cout << x << ","<
C.
void fun(int& a,int& b) {int x = a;a = b;b = x; } int main() {int x = 1, y = 2;fun(&x, &y);cout << x << ","<
D.
void fun(const int&a,const int&b) {int x = a;a = b;b = x; } int main() {int x = 1, y = 2;fun(x, y);cout << x << ","<
2-5
下面有一个结构体,对结构体的错误使用是( )
struct Book{string name;double price; }
A
Book b{"C++",20};
B
Book b; b->name="C++"; b->price=20;
C
Book *b=new Book[2]; b[0].name="C++"; b[0].price=20;
D
Book *p=new Book(); p->name="C++"; p->price=20;
2-6
下列种类的函数中,哪一种不是类的成员函数?
A.
构造函数
B.
析构函数
C.
友元函数
D.
拷贝构造函数
2-7
父类Base和子类Derive的定义如下。请问在子类中,继承的父类成员f,x,y的访问控制权限分别是:
class Base {public:void f();protected:int x;private:int y; };class Derive : protected Base { };
A.
public, protected, private
B.
public, public, public
C.
private, private, private
D.
protected, protected, private
2-8
当变量x的输入值是9时,下面程序的运行结果是:
#include
#include using namespace std; int main() {int x;cin>>x;try{cout<<"begin"< 100){cout<<"1"< A
begin12end
B
begin13: too big.2end
C
beginx=92end
D
begin13: too big.end
2-9
下列哪个代码不会调用对象的拷贝构造函数( C)
A.
MyClass a;
MyClass b=a;B.
MyClass a;
MyClass b(a);C.
MyClass a;
MyClass& b=a;D.
void f(MyClass obj)
{....
}
MyClass a;
f(a);
2-10根据下列类模板声明,正确初始化对象的方式是( A)
template
class MyClass{private:T1 x;T2 y;public:MyClass(T1 _x, T2 _y):x(_x),y(_y){} };
A.
MyClass a(10,'a');B.
MyClass a(10,'a');C.
MyClass a;D.
MyClass a;
Fn函数题
6-1 类的定义(教师类Teacher)
分数 10
全屏浏览题目
切换布局
作者 刘骥
单位 重庆大学
本题要求定义一个教师类Teacher,数据成员包含姓名name和年龄age,类的声明见给出的代码,请给出类的完整实现,并通过测试程序。
类的声明:
class Teacher{private:string name;int age;public:Teacher(string name,int age);string getName() const;int getAge() const ;void setName(string name);void setAge(int age); };
测试程序:
#include
#include using namespace std; class Teacher{private:string name;int age;public:Teacher(string name,int age);string getName() const;int getAge() const ;void setName(string name);void setAge(int age); };/* 请在这里填写答案 */int main(){Teacher a("Wang",20);cout<<"name:"< 测试程序的输入:
测试程序的输出:
name:Wang age:20 name:Zhang age:30
注意:“:”是英文冒号
提示
下列代码为类实现的骨架代码
Teacher::Teacher(string name,int age){//代码 } string Teacher::getName() const{//代码 } int Teacher::getAge() const{//代码 } void Teacher::setName(string name) {//代码 } void Teacher::setAge(int age) {//代码 }
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
参考答案
/* 请在这里填写答案 */ Teacher::Teacher(string name, int age) {//代码this->name = name;this->age = age; } string Teacher::getName() const {//代码return name; } int Teacher::getAge() const {//代码return age; } void Teacher::setName(string name) {//代码this->name = name; } void Teacher::setAge(int age) {//代码this->age = age; }
6-2 加、不等和输入输出的运算符重载(2维向量Vec2)
分数 10
全屏浏览题目
切换布局
作者 刘骥
单位 重庆大学
本题要求定义一个二维向量类Vec2,类的声明见给出的代码,请给出类的完整实现,并通过测试程序。类的声明包括如下内容:
- 数据成员,向量的第一维u和第二维v;
- u和v的访问函数;
- 构造函数;
- 加号、减号运算符重载(遵守二维向量的运算规则);
- ==和!=运算符重载(遵守二维向量的运算规则)
- 输入、输出运算符重载。
类实现代码需要补充构造函数以及加、不等、输入输出运算。
类的声明:
class Vec2{private:double u;double v;public:Vec2(double u=0,double v=0);double getU() const;double getV() const;Vec2 operator+(const Vec2&b);friend Vec2 operator-(const Vec2&a,const Vec2&b);bool operator==(const Vec2&b) const;friend bool operator!=(const Vec2&a,const Vec2&b);friend ostream&operator<<(ostream&os,const Vec2&c);friend istream&operator>>(istream&is,Vec2&c); };
测试程序:
#include
using namespace std; class Vec2{private:double u;double v;public:Vec2(double u=0,double v=0);double getU() const;double getV() const;Vec2 operator+(const Vec2&b);friend Vec2 operator-(const Vec2&a,const Vec2&b);bool operator==(const Vec2&b) const;friend bool operator!=(const Vec2&a,const Vec2&b);friend ostream&operator<<(ostream&os,const Vec2&c);friend istream&operator>>(istream&is,Vec2&c); }; double Vec2::getU() const {return u; } double Vec2::getV() const {return v; } Vec2 operator-(const Vec2&a,const Vec2&b){return Vec2(a.u-b.u,a.v-b.v); } bool Vec2::operator==(const Vec2&b) const{return u==b.u&&v==b.v; } /* 请在这里填写答案 */int main(){Vec2 a;cin>>a;cout<<"a: "< 测试程序的输入:
10 5
测试程序的输出:
a: u=10,v=5 c: u=13,v=9 d: u=7,v=1 a==a: 1 a!=a: 0
注意:“:”和“,”为英文,“:”后有一个空格
提示
下列代码为类实现的骨架代码
Vec2::Vec2(double u,double v){//代码 } Vec2 Vec2::operator+(const Vec2&b){//代码 } bool operator!=(const Vec2&a,const Vec2&b) {//代码 } ostream&operator<<(ostream&os,const Vec2&c){//代码 } istream&operator>>(istream&is,Vec2&c){//代码 }
参考答案
Vec2::Vec2(double u, double v) {//代码this->u = u;this->v = v; } Vec2 Vec2::operator+(const Vec2& b) {//代码Vec2 temp;temp.u = this->u + b.u;temp.v = this->v + b.v;return temp; } bool operator!=(const Vec2& a, const Vec2& b) {//代码if (a.u == b.u && a.v == b.v){return 0;}return 1; } ostream& operator<<(ostream& os, const Vec2& c) {//代码os << "u=" << c.u << ",v=" << c.v;return os; } istream& operator>>(istream& is, Vec2& c) {//代码is >> c.u >> c.v;return is; }
6-3 继承和多态(水果和香蕉)
请设计水果和香蕉类,并通过测试程序,具体要求如下:
- 水果(Fruit)是基类,成员包含:
- 保护成员变量重量(weight,int类型)
- 公有构造函数
- 公有析构函数
- 公有函数display
- 香蕉(Banana)从水果类公有继承,成员包含:
- 私有成员变量产地(origin,string类型)
- 公有构造函数
- 公有析构函数
- 公有函数display
对应代码
Fruit f(10); f.display();
输出为:
Fruit Constructor weight=10 Fruit Destructor
对应代码
Banana a("Chongqing",10); a.display();
输出为:
Fruit Constructor Banana Constructor origin=Chongqing,weight=10 Banana Destructor Fruit Destructor
对应代码
Fruit *pf=new Banana("Chongqing",10);; pf->display(); delete pf;
输出为:
Fruit Constructor Banana Constructor origin=Chongqing,weight=10 Banana Destructor Fruit Destructor
测试程序
#include
#include using namespace std;/* 请在这里填写答案 */ int main(){Fruit *pf=new Banana("Chongqing",10);;pf->display();delete pf;return 0; } 测试程序的输入
测试程序的输出
Fruit Constructor Banana Constructor origin=Chongqing,weight=10 Banana Destructor Fruit Destructor
参考答案
class Fruit { public:Fruit(){cout << "Fruit Constructor" << endl;}Fruit(int a){cout << "Fruit Constructor" << endl;weight = a;}virtual void display(){cout << "weight=" << weight << endl;}virtual ~Fruit(){cout << "Fruit Destructor" << endl;} protected:int weight; };class Banana :public Fruit { public:Banana(string str, int weit){cout << "Banana Constructor" << endl;weight = weit;origin = str;}void display(){cout << "origin=" << origin << ",weight=" << weight << endl;}~Banana(){cout << "Banana Destructor" << endl;} private:string origin; };
编程题
7-1 约数
分数 10
全屏浏览题目
切换布局
作者 葛亮
单位 重庆大学
当整数a除以整数b(b≠0),除得的商正好是整数而没有余数时,称b是a的约数。给你两个正整数A和B, 如果B是A的约数,请打印输出A+B, 否则打印输出A−B。
输入格式:
输入包括两个正整数A和B(1≤B≤A≤100)。
输出格式:
输出结果。
输入样例1:
12 4
输出样例1:
16
输入样例2:
20 8
输出样例2:
12
输入样例3:
1 1
输出样例3:
2
#include
using namespace std;int main() {int a, b;cin >> a >> b;bool flag = 0;if (a % b == 0){flag = 1;}if (flag == 1){cout << a + b;}else{cout << a - b;}}
7-2 逆序整数
分数 10
全屏浏览题目
切换布局
作者 葛亮
单位 重庆大学
对于n位的两个正整数x和y,可以把x表示为x1x2…xn,其中xi(1≤i≤n)表示整数x第i位上的数字;把y表示为y1y2…yn,其中yi(1≤i≤n)表示整数y第i位上的数字。如果x1+k=yn−k(0≤k≤n−1)始终成立,则x和y互为逆序整数。
给你两个正整数A和B, 判断两个整数是否互为逆序整数,并输出相应的结果。
注:题目保证正整数A和B的个位数字不是0。
输入格式:
先在第一行输入位数n,然后在接下来的两行分别输入位数为n的正整数A和B(1≤n≤50)。
输出格式:
输入样例1:
2 89 98
输出样例1:
89 and 98 are reverse.
输入样例2:
3 981 289
输出样例2:
981 and 289 are not reverse.
提示:
输入的两个正整数可能会超过long long int所能表示数的范围。可以用字符串来表示正整数。
输出结果以“.”结尾。
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include
using namespace std; int main() {int n; string A; string B;cin >> n;cin >> A >> B;int cishu = 0;for (int k = 0; k < n; k++){if (A[k] == B[n - 1 - k]){cishu;}else { cishu++;}}if (cishu == 0) { cout << A << " and " << B << " are reverse.";}else {cout << A << " and " << B << " are not reverse."; } }
7-3 一维世界的疫情传播
分数 10
全屏浏览题目
切换布局
作者 葛亮
单位 重庆大学
可怕的阿尔法病毒正在一维世界传播,如果某人的活动范围与病毒携带者的活动范围有交集,则有被感染的可能。假设有一名病毒携带者A在位于X的家附近活动,他到过的地方用xi表示(这些地点不重复),对于X和xi之间的区域,都认为是A的活动范围。另外有一名健康者B,他在位于Y的家附近活动,他到过的地方用yi表示(这些地点不重复),对于Y和yi之间的区域,都认为是B的活动范围。所有的地点都位于整数点上,−100≤X,Y,xi,yi≤100,X=Y,xi=X,yi=Y。现在需要判断B是否有被感染的可能。
输入格式:
第一行输入N,M,X,Y四个整数,接下来2行分别是N个xi和M个yi的值。整数值之间都用空格隔开。具体如下:
NMXY
x1x2…xN
y1y2…yM
输出格式:
用家的位置表示A和B两人,输出是否有被感染的可能,具体格式请参见样例。
输入样例1:
3 2 10 20 8 15 13 16 22
输出样例1:
10 and 20: impossible
输入样例2:
4 2 -48 -1 -20 -35 -91 -23 -22 66
输出样例2:
-48 and -1: possible
输入样例3:
5 3 6 8 -10 3 1 5 -100 100 6 14
输出样例3:
6 and 8: possible
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include
using namespace std; int main() {int N, M, X, Y;cin >> N >> M >> X >> Y;int* x = new int[N];int* y = new int[M];//lipu int x_Max = X, x_Min = X;int y_Max = Y, y_Min = Y;for (int i = 0; i < N;i++){cin >> x[i];if (x[i] > x_Max){x_Max = x[i];}if (x[i] < x_Min){x_Min = x[i];}//找到x小组数据区间x_Min-x_Max;}for (int i = 0; i < M; i++){cin >> y[i];if (y[i] > y_Max){y_Max = y[i];}if (y[i] < y_Min){y_Min = y[i];} //找到y小组数据区间x_Min-x_Max;}bool isPossible = 0;if (x_Max < y_Min || y_Max < x_Min){isPossible = 0;}else{isPossible = 1;}if (isPossible == 1){cout << X << " and " << Y << ": possible";}else{cout << X << " and " << Y << ": impossible";}return 0; }