网易2011笔试题详解

来源:互联网 发布:评标专家库软件 编辑:程序博客网 时间:2024/06/07 02:58

http://blog.csdn.net/silangquan/article/details/18051675


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


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语言把双向链表中的两个结点交换位置,考虑各种边界问题。 

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. struct Node{    
  2.     Node* prev;       
  3.     Node* next;       
  4.     void* data;       
  5. };    
  6.     
  7. struct LinkedList{    
  8.     Node*    head;    
  9.     Node*    tail;    
  10.     Node*    cur;    
  11.     int      size;    
  12. };   
  13.   
  14. bool exchange(LinkedList* list,Node *node1,Node *node2)   
  15. {   
  16.     if(node1== NULL || node2==NULL)   
  17.     return false;  
  18.     Node *p,*q;  
  19.     //node1 on the front  
  20.     if(list->head->next == node1)  
  21.     {  
  22.         //node2 on the last  
  23.         if(list->tail->next == node2)  
  24.         {  
  25.             p = node2->prev;  
  26.             //Cope with node2  
  27.             list->head->next = node2;  
  28.             node2->prev = list->head;  
  29.             node2->next = node1->next;  
  30.             node2->next->pre = node2;  
  31.             //Cope with node1  
  32.             list->tail->prev = node1;  
  33.             node1->next = list->tail;  
  34.             node1->prev = p;  
  35.             p->next = node1;  
  36.             return true;  
  37.         }  
  38.         //node2 not on the last  
  39.         else  
  40.         {  
  41.             p = node2->prev;  
  42.             q = node2->next;  
  43.             //Cope with node2  
  44.             list->head->next = node2;  
  45.             node2->prev = list->head;  
  46.             node2->next = node1->next;  
  47.             node2->next->prev = node2;  
  48.             //Cope with node1  
  49.             p->next = node1;  
  50.             node1->prev = p;  
  51.             node1->next = q;  
  52.             q->prev = node1;  
  53.             return true;  
  54.         }  
  55.           
  56.     }  
  57.     //node1 on the last  
  58.     else if(list->tail->next == node1)  
  59.     {  
  60.         //node2 on the front  
  61.         if(list->head->next == node2)  
  62.         {  
  63.             p = node1->prev;  
  64.             //Cope with node1  
  65.             list->head->next = node1;  
  66.             node1->prev = list->head;  
  67.             node1->next = node2->next;  
  68.             node1->next->prev = node1;  
  69.             //Cope with node2  
  70.             list->tail->prev = node2;  
  71.             node2->next = list->tail;  
  72.             node2->prev = p;  
  73.             p->next = node2;  
  74.             return true;  
  75.         }  
  76.         //node2 not on the front  
  77.         else  
  78.         {  
  79.             p = node2->prev;  
  80.             q = node2->next;  
  81.             //Cope with node2  
  82.             list->tail->next = node2;  
  83.             node2->prev = list->tail;  
  84.             node2->next = node1->next;  
  85.             node2->next->prev = node2;  
  86.             //Cope with node1  
  87.             p->next = node1;  
  88.             node1->prev = p;  
  89.             node1->next = q;  
  90.             q->prev = node1;  
  91.             return true;  
  92.         }  
  93.     }  
  94.     //node1 on the middle  
  95.     else  
  96.     {  
  97.         //node2 on the front  
  98.         if(list->head->next == node2)  
  99.         {  
  100.             p = node1->prev;  
  101.             q = node1->next;  
  102.             node1->prev = list->head;  
  103.             list->head->next = node1;  
  104.             node1->next = node2->next;  
  105.             node2->next->prev = node1;  
  106.               
  107.             node2->prev = p;  
  108.             p->next = node2;  
  109.             node2->next = q;  
  110.             q->prev = node2;  
  111.         }  
  112.         //node2 on the last  
  113.         else if(list->tail->next == node2)  
  114.         {  
  115.             p = node1->prev;  
  116.             q = node1->next;  
  117.             node1->prev = node2->prev;  
  118.             node2->prev->next = node1;  
  119.             node1->next = list->tail;  
  120.             list->tail->prev = node1;  
  121.               
  122.             node2->prev = p;  
  123.             p->next = node2;  
  124.             node2->next = q;  
  125.             q->prev = node2;  
  126.         }  
  127.         //both in the middle  
  128.         else  
  129.         {  
  130.             p = node2->prev;  
  131.             q = node2->next;  
  132.             //Cope with node2  
  133.             node2->prev = node1->prev;  
  134.             node1->prev->next = node2;  
  135.             node2->next = node1->next;  
  136.             node1->next->prev = node2;  
  137.             //Cope with node1  
  138.             p->next = node1;  
  139.             node1->prev = p;  
  140.             node1->next = q;  
  141.             q->prev = node1;  
  142.             return true;  
  143.         }  
  144.     }  
  145.   
  146. }   




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.输出结果为?

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3. class A   
  4. {  
  5. public:  
  6.     A(){cout<<"1";}  
  7.     A(A &a){cout <<"2";}  
  8.     virtual ~A() {cout<<"3";}  
  9. };  
  10.   
  11. class B:public A  
  12. {  
  13. public:  
  14.     B(){cout<<"4";}  
  15.     B(B &b){cout<<"5";}  
  16.     ~B(){cout<<"6";}  
  17. };  
  18.   
  19. int main()  
  20. {  
  21.     A* pa = new B();  
  22.     delete pa;  
  23.     return 0;  
  24. }  


1463

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

0 0