一、简答题
(1)一位老师有2个推理能力很强的学生,他告诉学生他手里有以下的牌:
黑桃:2 , 5 , 7 , 9 , J , K
红心:3 , 4 , 9 , J , K
梅花:5 , 8 , 9 , Q
方块:2 , 7 , 8
然后从中拿出一张牌,告诉A这张牌的大小,告诉了B这张牌的花色;
A:我不知道这张是什么牌
B:我就知道你肯定不知道这张是什么牌
A:现在我知道
B:现在我也知道了
请问这张是什么牌?
答:方块8
(2)有11个乒乓球,其中有一个球是伪劣产品并存在质量较轻的问题,现有一个没有砝码的天平,只能称3次把那个假货给称出来。
答:
第一次,天平两端各放5个乒乓球,如果天平平衡,那么剩下的那个就是伪劣产品。
如果不平衡,则将天平较轻那端的5个乒乓球选出来,然后在天平两端各放2个乒乓球,如果天平平衡,那么剩下的那个就是伪劣产品。否则,将天平较轻那端的2个乒乓球选出来,放在天平上重新测量,天平较轻端的那个乒乓球就是伪劣产品。
(3)说明指针与引用的区别。
答:●指针是一个实体,而引用仅是个别名;
●引用只能在定义时被初始化一次,之后不可变;指针可变;引用“从一而终”,指针可以“见异思迁”;
●引用没有const,指针有const,const的指针不可变;
●引用不能为空,指针可以为空;
●“sizeof 引用”得到的是所指向的变量(对象)的大小,而“sizeof 指针”得到的是指针本身的大小;
●指针和引用的自增(++)运算意义不一样;
●引用是类型安全的,而指针不是 (引用比指针多了类型检查
从内存分配上看:程序为指针变量分配内存区域,而引用不分配内存区域。指针:指向另一个内存空间的变量,我们可以通过它来索引另一个内存空间的内容,本身有自己的内存空间。
(4)列出C++类型转换操作符,并分别举例。
dynamic_cast: 在多态类型转换时使用,用来执行继承体系中"安全的向下转型或跨系转型动作",就是子类对象指针转化为父类对象指针。实现在运行时,并进行运行时检测,如果转换失败,返回值是NULL。
static_cast:与dynamic_cast相反,static_cast是在编译时转换类型的,故称为static_cast,它可以用在值类型转换中
const_cast:一般用于去除const, volatile等修饰属性上.
reinterpret_cast:特意用于底层的强制转型,这个操作符能够在非相关的类型之间转换。操作结果只是简单的从一个指针到别的指针的值的二进制拷贝。在类型之间指向的内容不做任何类型的检查和转换。
(5)写个简单的函数,用于判断CPU的字节序(little endian/big endian)
(6)实现一个128位整数的类,并且完成后面的函数,测试一个数是否为素数。
class int128
{
};
bool isPrime(int128 & number)
{
...
}
答:
(7)对 二叉树进行排序,排序后的结果为二叉排序树。
二叉排序树又称二叉查找树,它或者是一棵空树,或者是具有下列性质的二叉树:(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;(3)左、右子树也分别为二叉排序树;
struct STreeNode
{
int key;
STreeNode* left_child;
STreeNode* right_child;
};
//返回值为排序后的根节点
STreeNode* bt2bst(STreeNode* root_node)
{
}
(1)一位老师有2个推理能力很强的学生,他告诉学生他手里有以下的牌:
黑桃:2 , 5 , 7 , 9 , J , K
红心:3 , 4 , 9 , J , K
梅花:5 , 8 , 9 , Q
方块:2 , 7 , 8
然后从中拿出一张牌,告诉A这张牌的大小,告诉了B这张牌的花色;
A:我不知道这张是什么牌
B:我就知道你肯定不知道这张是什么牌
A:现在我知道
B:现在我也知道了
请问这张是什么牌?
答:方块8
(2)有11个乒乓球,其中有一个球是伪劣产品并存在质量较轻的问题,现有一个没有砝码的天平,只能称3次把那个假货给称出来。
答:
第一次,天平两端各放5个乒乓球,如果天平平衡,那么剩下的那个就是伪劣产品。
如果不平衡,则将天平较轻那端的5个乒乓球选出来,然后在天平两端各放2个乒乓球,如果天平平衡,那么剩下的那个就是伪劣产品。否则,将天平较轻那端的2个乒乓球选出来,放在天平上重新测量,天平较轻端的那个乒乓球就是伪劣产品。
(3)说明指针与引用的区别。
答:●指针是一个实体,而引用仅是个别名;
●引用只能在定义时被初始化一次,之后不可变;指针可变;引用“从一而终”,指针可以“见异思迁”;
●引用没有const,指针有const,const的指针不可变;
●引用不能为空,指针可以为空;
●“sizeof 引用”得到的是所指向的变量(对象)的大小,而“sizeof 指针”得到的是指针本身的大小;
●指针和引用的自增(++)运算意义不一样;
●引用是类型安全的,而指针不是 (引用比指针多了类型检查
从内存分配上看:程序为指针变量分配内存区域,而引用不分配内存区域。指针:指向另一个内存空间的变量,我们可以通过它来索引另一个内存空间的内容,本身有自己的内存空间。
(4)列出C++类型转换操作符,并分别举例。
dynamic_cast: 在多态类型转换时使用,用来执行继承体系中"安全的向下转型或跨系转型动作",就是子类对象指针转化为父类对象指针。实现在运行时,并进行运行时检测,如果转换失败,返回值是NULL。
static_cast:与dynamic_cast相反,static_cast是在编译时转换类型的,故称为static_cast,它可以用在值类型转换中
const_cast:一般用于去除const, volatile等修饰属性上.
reinterpret_cast:特意用于底层的强制转型,这个操作符能够在非相关的类型之间转换。操作结果只是简单的从一个指针到别的指针的值的二进制拷贝。在类型之间指向的内容不做任何类型的检查和转换。
(5)写个简单的函数,用于判断CPU的字节序(little endian/big endian)
(6)实现一个128位整数的类,并且完成后面的函数,测试一个数是否为素数。
class int128
{
};
bool isPrime(int128 & number)
{
...
}
答:
(7)对 二叉树进行排序,排序后的结果为二叉排序树。
二叉排序树又称二叉查找树,它或者是一棵空树,或者是具有下列性质的二叉树:(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;(3)左、右子树也分别为二叉排序树;
struct STreeNode
{
int key;
STreeNode* left_child;
STreeNode* right_child;
};
//返回值为排序后的根节点
STreeNode* bt2bst(STreeNode* root_node)
{
}