剑指XX游戏(二) - 网易2011笔试题详解

本文详细介绍了C++编程的基础知识,包括二维数组的内存布局、用户级线程与内核级线程的区别、从C++文件到生成exe文件的步骤、双向链表操作、DLL、LIB、EXE文件的区别、八叉树算法、排列组合问题、宠物技能遗传概率计算、构造函数与析构函数的使用等主题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上弄到的一份题,不是很完整,边猜边做。


1.写出运行结果

char array[] = “abcde”; char* s = array;

cout<<sizeof(array)<<strlen(array)<<sizeof(s)<<strlen(s);


6585


2.什么是用户级线程和内核级线程?区别。

内核级线程:
(1)线程的创建、撤销和切换等,都需要内核直接实现,即内核了解每一个作为可调度实体的线程。
(2)这些线程可以在全系统内进行资源的竞争。
(3)内核空间内为每一个内核支持线程设置了一个线程控制块(TCB),内核根据该控制块,感知线程的存在,并进行控制。
在一定程度上类似于进程,只是创建、调度的开销要比进程小。有的统计是1:10
用户级线程:
(1)用户级线程仅存在于用户空间。——>对比内核(3)
(2)内核并不能看到用户线程。——>重要的区别

(3)内核资源的分配仍然是按照进程进行分配的;各个用户线程只能在进程内进行资源竞争。


3.从C++文件到生成exe 文件经过哪三个步骤?

预编译,编译优化,汇编,链接


4.有个二维数组 A(6*8),每个元素占 6 字节,起始地址为 1000,请问最后一个元素 A[5][7]的起始地址为??? 数组A占内存大小为??? 假设以行优先,则A[1][4]起始地址为???

1)1000 + 6*6*8 - 8 = 11282; 2)6*6*8=288; 3)A[1][4]位置为5行2列,1000+6*(8*1+4) = 1272.


如果给出结构体,考虑到字节对齐的话就要另外考虑了。



5.用C语言把双向链表中的两个结点交换位置,考虑各种边界问题。 

考虑三种情况:第一个结点在头,第一个结点在中间,第一个结点在尾巴。

struct Node{  
    Node* prev;     
    Node* next;     
    void* data;     
};  
  
struct LinkedList{  
    Node*    head;  
    Node*    tail;  
    Node*    cur;  
    int      size;  
}; 

bool exchange(LinkedList* list,Node *node1,Node *node2) 
{ 
    if(node1== NULL || node2==NULL) 
    return false;
    Node *p,*q;
	//node1 on the front
    if(list->head->next == node1)
    {
    	//node2 on the last
    	if(list->tail->next == node2)
    	{
    		p = node2->prev;
    		//Cope with node2
    		list->head->next = node2;
    		node2->prev = list->head;
    		node2->next = node1->next;
    		node2->next->pre = node2;
    		//Cope with node1
    		list->tail->prev = node1;
    		node1->next = list->tail;
    		node1->prev = p;
    		p->next = node1;
    		return true;
    	}
    	//node2 not on the last
    	else
    	{
    		p = node2->prev;
    		q = node2->next;
    		//Cope with node2
    		list->head->next = node2;
    		node2->prev = list->head;
    		node2->next = node1->next;
    		node2->next->prev = node2;
    		//Cope with node1
    		p->next = node1;
    		node1->prev = p;
    		node1->next = q;
    		q->prev = node1;
    		return true;
    	}
		
    }
    //node1 on the last
    else if(list->tail->next == node1)
    {
    	//node2 on the front
    	if(list->head->next == node2)
    	{
			p = node1->prev;
    		//Cope with node1
    		list->head->next = node1;
    		node1->prev = list->head;
    		node1->next = node2->next;
    		node1->next->prev = node1;
    		//Cope with node2
    		list->tail->prev = node2;
    		node2->next = list->tail;
    		node2->prev = p;
    		p->next = node2;
    		return true;
    	}
    	//node2 not on the front
    	else
    	{
    		p = node2->prev;
    		q = node2->next;
    		//Cope with node2
    		list->tail->next = node2;
    		node2->prev = list->tail;
    		node2->next = node1->next;
    		node2->next->prev = node2;
    		//Cope with node1
    		p->next = node1;
    		node1->prev = p;
    		node1->next = q;
    		q->prev = node1;
    		return true;
    	}
    }
    //node1 on the middle
    else
    {
    	//node2 on the front
    	if(list->head->next == node2)
    	{
    		p = node1->prev;
    		q = node1->next;
    		node1->prev = list->head;
    		list->head->next = node1;
    		node1->next = node2->next;
    		node2->next->prev = node1;
    		
    		node2->prev = p;
    		p->next = node2;
    		node2->next = q;
    		q->prev = node2;
    	}
    	//node2 on the last
    	else if(list->tail->next == node2)
    	{
    		p = node1->prev;
    		q = node1->next;
    		node1->prev = node2->prev;
    		node2->prev->next = node1;
    		node1->next = list->tail;
    		list->tail->prev = node1;
    		
    		node2->prev = p;
    		p->next = node2;
    		node2->next = q;
    		q->prev = node2;
    	}
    	//both in the middle
    	else
    	{
    		p = node2->prev;
    		q = node2->next;
    		//Cope with node2
    		node2->prev = node1->prev;
    		node1->prev->next = node2;
    		node2->next = node1->next;
    		node1->next->prev = node2;
    		//Cope with node1
    		p->next = node1;
    		node1->prev = p;
    		node1->next = q;
    		q->prev = node1;
    		return true;
    	}
    }

} 




6.*.dll,*.lib,*.exe 文件分别是什么,有什么区别?

lib是静态的库文件,dll是动态的库文件。
所谓静态就是link的时候把里面需要的东西抽取出来安排到你的exe文件中,以后运行exe的时候不再需要lib。
所谓动态就是exe运行的时候依赖于dll里面提供的功能,没有这个dll,exe无法运 行。

lib, dll, exe都算是最终的目标文件,是最终产物。而c/c++属于源代码。源代码和最终 目标文件中过渡的就是中间代码obj,实际上之所以需要中间代码,是你不可能一次得到目 标文件。比如说一个exe需要很多的cpp文件生成。而编译器一次只能编译一个cpp文件。这 样编译器编译好一个cpp以后会将其编译成obj,当所有必须要的cpp都编译成obj以后,再统 一link成所需要的exe,应该说缺少任意一个obj都会导致exe的链接失败.



7.附加题(20):使用八叉树算法把24位真彩色转化成 256色。24位真彩色包括 R,G,B颜色,每种颜色8 位。

         在计算机中像素的计算单位一般是二进制的,256色,即2的8次方,因此我们也把256色图形叫做8位图;16位图,它可以表达2的16次方即65536种颜色;还有24位彩色图,可以表达16,777,216种颜色。

         算法参考:http://blog.csdn.net/zuzubo/article/details/1597985



8.有 11 盆花,围成一圈,要求每次组合时,每盆花相邻的两盆花与上次不同,请问有多少排列方法? 

待解答。


9.2 只宠物合成,1只有 5技能,1 只有4 技能,每个技能有 a%概率遗传,请问刚好有7 个技能遗传成功的概率是?

只有

第一只5个技能 + 第二只2个技能:(a%)^7*C(4,2)

第一只4个技能 + 第二只3个技能:(a%)^7*C(5,4)*C(4,3)

第一只3个技能 + 第二只4个技能:(a%)^7*C(5,3)

加起来就可以了。


10.输出结果为?

#include <iostream>
using namespace std;
class A	
{
public:
	A(){cout<<"1";}
	A(A &a){cout <<"2";}
	virtual ~A() {cout<<"3";}
};

class B:public A
{
public:
	B(){cout<<"4";}
	B(B &b){cout<<"5";}
	~B(){cout<<"6";}
};

int main()
{
    A* pa = new B();
	delete pa;
	return 0;
}


1463

子类构造之前首先调用基类的构造函数,然后是子类的构造函数,析构的时候相反,注意基类的析构函数声明为virtual才可以.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值