笔试强训48天——day14

一. 单选

1. 下列有关this指针使用方法的叙述正确的是()

A 保证基类保护成员在子类中可以被访问
B 保证基类私有成员在子类中可以被访问
C 保证基类共有成员在子类中可以被访问
D 保证每个对象拥有自己的数据成员,但共享处理这些数据的代码

正确答案:D

this指针是当前对象的地址

 

2.有一个类B继承自类A,他们数据成员如下:则构造函数中,成员变量一定要通过初始化列表来初始化的是____。

class A {
...
privateint a;
};
class B : public A {
...
private:
int a;
public:
const int b;
A &c;
static const char* d;
B* e;
} 

A a b c
B b c e
C b c d e
D c e
E b d
F b c
正确答案:F

常量、引用都需要参数列表初始化

 

3. 下面说法正确的是()

A C++已有的任何运算符都可以重载
B const对象只能调用const类型成员函数
C 构造函数和析构函数都可以是虚函数
D 函数重载返回值类型必须相同

正确答案:B

. .* :: sizeof ?: 五个运算符都不能重载
函数重载与返回值没有关系,主要是参数列表

 

4. 下面说法正确的是()

A 一个空类默认一定生成构造函数,拷贝构造函数,赋值操作符,引用操作符,析构函数
B 可以有多个析构函数
C 析构函数可以为virtual,可以被重载
D 类的构造函数如果都不是public访问属性,则类的实例无法创建

正确答案:A

析构函数——只能有一个
析构函数可以为virtual,可以被重载——不可以被重载

 

5. 面向对象设计中的继承和组合,下面说法错误的是?()

A 继承允许我们覆盖重写父类的实现细节,父类的实现对于子类是可见的,是一种静态复用,也称为白
盒复用
B 组合的对象不需要关心各自的实现细节,之间的关系是在运行时候才确定的,是一种动态复用,也称
为黑盒复用
C 优先使用继承,而不是组合,是面向对象设计的第二原则
D 继承可以使子类能自动继承父类的接口,但在设计模式中认为这是一种破坏了父类的封装性的表现

正确答案:C

优先使用的是组合

 

6. 关于重载和多态正确的是

A 如果父类和子类都有相同的方法,参数个数不同,将子类对象赋给父类后,由于子类继承于父类,所以使用
父类指针 调用父类方法时,实际调用的是子类的方法
B 选项全部都不正确
C 重载和多态在C++面向对象编程中经常用到的方法,都只在实现子类的方法时才会使用
D

class A{
void test(float a){cout<<"1";}
};
class B:public A{
void test(int b){cout<<"2";}
};
A *a=new A;
B *b=new B;
a=b;
a.test(1.1);

结果是1

正确答案:B

D——语法错误

 

7.以下程序的输出是()

class Base {
public:
Base(int j): i(j) {}
virtual~Base() {}
void func1() {
i *= 10;
func2();
}
 i
nt getValue() {
return i;
} p
rotected:
virtual void func2() {
i++;
} p
rotected:
int i;
};
class Child: public Base {
public:
Child(int j): Base(j) {}
void func1() {
i *= 100;
func2();
} p
rotected:
void func2() {
i += 2;
}
};
int main() {
Base * pb = new Child(1);
pb->func1();//10
cout << pb->getValue() << endl; delete pb;
} 

A 11
B 101
C 12
D 102
正确答案:C

 

8.下面 C++ 代码的运行结果为()

#include<iostream>
#include<string>
using namespace std;
class B0 {
public:
virtual void display() {
cout << "B0::display0" << endl;
}
};
class B1 :public B0 {
public:
void display() { cout << "B1::display0" << endl; }
};
class D1 : public B1 {
public:
void display() {
cout << "D1::display0" << endl;
}
};
void fun(B0 ptr) {
ptr.display();
}

  i
nt main() {
B0 b0;
B1 b1;
D1 d1;

fun(b0);
fun(b1);
fun(d1);
} 

A B0::display0 B0::display0 B0::display0
B B0::display0 B0::display0 D1::display0
C B0::display0 B1::display0 D1::display0
D B0::display0 B1::display0 B1::display0
正确答案:A

全局函数是通过父类对象调用的——通过对象调动是静态——无论调父类还是子类都是用父类的方法实现

 

9. 下列哪种函数可以定义为虚函数()

A 构造函数
B 析构函数
C 内联成员函数
D 静态成员函数

正确答案:B

 

10.下面 C++ 程序的运行结果为()

#include<iostream>
using namespace std;
class A {
public: A(const char* s) { cout << s << endl; } ~A() {}
};
class B : virtual public A {
public: B(const char* s1, const char* s2) :A(s1) { cout << s2 << endl; }
};
class C : virtual public A {
public: C(const char* s1, const char* s2) :A(s1) { cout << s2 << endl; }
};
class D : public B, public C {
public: D(const char* s1, const char* s2, const char* s3, const char* s4) :B(s1, s2), C(s1, s3), A(s1)
{ cout << s4 << endl; }
};
int main() 
{ D* p = new D("class A", "class B", "class C", "class D"); delete p; return 0;
}

A class A
class B
class C
class D
B class D
class B
class C
class A
C class D
class C
class B

D class A
class C
class B
class D
正确答案:A

菱形继承
先构造父类

 

 

二. 编程

1. 计算日期到天数转换

链接

根据输入的日期,计算是这一年的第几天。
保证年份为4位数且日期合法。

输入描述:
输入一行,每行空格分割,分别是年,月,日
输出描述:
输出是这一年的第几天

示例1:
输入
2012 12 31
输出
366

示例2:
输入
1982 3 4
输出
63

正确答案:

#include<iostream>
using namespace std;

//判断闰年
bool isLeap(int year)
{
return (year%4==0&&year%100!=0) || (year%400==0);
} 

//根据年和月获取相应月份的天数
int getDaysByYM(int year, int month)
{
int days[13] = {29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(month==2 && isLeap(year))
month = 0;
return days[month];
}
 
 //根据年月日获取相应的这一年的第几天
int getDaysByYMD(int year, int month, int day)
{
int days = 0;
for(int i=1; i<month; ++i)
{
days += getDaysByYM(year, i);
} days += day;
return days;
} 

int main()
{
int year, month, day;
int days = 0;
while(cin>>year>>month>>day)
{
days = getDaysByYMD(year, month, day);
cout<<days<<endl;
} 
return 0;
}

 

2.幸运的袋子

添加链接描述

一个袋子里面有n个球,每个球上面都有一个号码(拥有相同号码的球是无区别的)。如果一个袋子是幸运的当
且仅当所有球的号码的和大于所有球的号码的积。
例如:如果袋子里面的球的号码是{1, 1, 2, 3},这个袋子就是幸运的,因为1 + 1 + 2 + 3 > 1 * 1 * 2 * 3
你可以适当从袋子里移除一些球(可以移除0个,但是别移除完),要使移除后的袋子是幸运的。现在让你编程计
算一下你可以获得的多少种不同的幸运的袋子。

输入描述:
第一行输入一个正整数n(n ≤ 1000)
第二行为n个数正整数xi(xi ≤ 1000)
输出描述:
输出可以产生的幸运的袋子数

示例1:
输入
3 1
1 1
输出
2

正确答案:

#include<iostream>
#include<algorithm>
using namespace std;
/*
getLuckyPacket:从当前位置开始搜索符合要求的组合,一直搜索到最后一个位置结束
x[]: 袋子中的所有球
n: 球的总数
pos: 当前搜索的位置
sum: 到目前位置的累加和
multi: 到目前位置的累积值
*/
int getLuckyPacket(int x[], int n, int pos, int sum, int multi)
{
int count = 0;
//循环,搜索以位置i开始所有可能的组合
for (int i = pos; i<n; i++)
{
sum += x[i];
multi *= x[i];
if (sum > multi)
{
//找到符合要求的组合,加1,继续累加后续的值,看是否有符合要求的集合
count += 1 + getLuckyPacket(x, n, i + 1, sum, multi);
} 
else if (x[i] == 1)
{
//如何不符合要求,且当前元素值为1,则继续向后搜索
count += getLuckyPacket(x, n, i + 1, sum, multi);
} 
else
{
//如何sum大于multi,则后面就没有符合要求的组合了
break;
} 
//要搜索下一个位置之前,首先恢复sum和multi
sum -= x[i];
multi /= x[i];
//数字相同的球,没有什么区别,都只能算一个组合,所以直接跳过
while (i < n - 1 && x[i] == x[i + 1])
{
i++;
}
} 
return count;
} 
int main()
{
int n;
while (cin >> n)
{
int x[n];
for (int i = 0; i < n; i++)
{
cin >> x[i];
} 
sort(x, x + n);
//从第一个位置开始搜索
cout << getLuckyPacket(x, n, 0, 0, 1) << endl;
}
 return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hey pear!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值